How to create a circular outlined Material Button in Android?

前端 未结 2 1275
时光说笑
时光说笑 2021-02-07 02:21

I am trying to create a button with an icon in the center. The top and bottom part of the circle are a little flat. Is there a way to do this without using corner radius? Here i

相关标签:
2条回答
  • 2021-02-07 03:09

    You can use the app:shapeAppearanceOverlay attribute to define the corner size. You can use the 50% value.

    <com.google.android.material.button.MaterialButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
        app:icon="@drawable/ic_add_24px"
        app:iconSize="24dp"
        app:iconGravity="textStart"
        android:padding="0dp"
        app:iconPadding="0dp"
        android:insetLeft="0dp"
        android:insetTop="0dp"
        android:insetRight="0dp"
        android:insetBottom="0dp"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Circle"
        />
    

    with:

      <style name="ShapeAppearanceOverlay.MyApp.Button.Circle" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">50%</item>
      </style>
    

    or with the style="@style/Widget.MaterialComponents.Button.Icon"

    It requires the version 1.1.0.

    0 讨论(0)
  • 2021-02-07 03:15

    By using cornerRadius along with inset you can get the rounded shape:

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        app:cornerRadius="30dp"
        android:insetTop="0dp"
        android:insetBottom="0dp"
        android:insetLeft="0dp"
        android:insetRight="0dp"
        app:icon="@drawable/ic_menu"/>
    
    0 讨论(0)
提交回复
热议问题