android - 如何在 Android 中创建圆形轮廓 Material 按钮?

标签 android android-layout android-button material-components material-components-android

我正在尝试在中心创建一个带有图标的按钮。圆圈的顶部和底部有点平坦。有没有办法在不使用圆角半径的情况下做到这一点?这是我的按钮布局。

<com.google.android.material.button.MaterialButton
        android:id="@+id/start_dispenser_btn"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="175dp"
        android:layout_height="175dp"
        android:padding="14dp"
        app:cornerRadius="150dp"
        app:icon="@drawable/ic_play_arrow_black_60dp"
        app:iconGravity="end"
        app:iconSize="150dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/stop_dispenser_btn"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/test_dispenser_container"
        app:strokeColor="@color/background_black" />

enter image description here

最佳答案

您可以使用 app:shapeAppearanceOverlay 属性来定义角的大小。您可以使用 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"
    />
和:
  <style name="ShapeAppearanceOverlay.MyApp.Button.Circle" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>
enter image description here
或使用 style="@style/Widget.MaterialComponents.Button.Icon" enter image description here
它至少需要 1.1.0 版本。

喷气背包组成 您可以使用 OutlinedButton 申请 CircleShape 作为形状:
    OutlinedButton(onClick = { /* ... */ },
        modifier= Modifier.size(50.dp), // it is important otherwise the button is oval
        shape = CircleShape,
        border= BorderStroke(1.dp, Color.Blue),
        contentPadding = PaddingValues(0.dp),
        colors = ButtonDefaults.outlinedButtonColors(contentColor =  Color.Blue)
    ) {
            Icon(Icons.Default.Add, contentDescription = "content description")
    }
enter image description here

关于android - 如何在 Android 中创建圆形轮廓 Material 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58529593/

相关文章:

android - onKeyDown() 或 onBackPressed()

Android - 应用程序未运行。错误 : The minCompileSdk specified in a dependency's AAR metadata is greater than this module's compileSdkVersion

android - 如何更改扩展 float 操作按钮的形状?

带有复选框的 Android ListView : How to capture checked items?

Android:layout_weight 在以编程方式设置时不起作用

android - AppCompatEditText 的默认背景是什么?

java - Android - 来自服务器的 JSON 为空

android - 如何测试演示者 MVP Android

android - 以编程方式在 API 级别 7 中设置 View 位置

java - 将 Activity 元素的排列设置为从左到右(默认)