我想把纽扣的角弄圆。在Android中是否有一种简单的方法来实现这一点?


当前回答

创建如下所示的XML文件。设置它为按钮的背景。改变半径属性为您的愿望,如果你需要更多的曲线按钮。

button_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/primary" />
    <corners android:radius="5dp" />
</shape>

为按钮设置背景:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_background"/>

其他回答

在Android中是否有一种简单的方法来实现这一点?

使用Jetpack Compose,你可以使用shape参数:

    Button( 
        onClick = { /* Do something! */ },
        shape = RoundedCornerShape(8.dp)
    ){
        Text("Button")
    }


在Material Components库中,你可以在app:cornerRadius属性中使用MaterialButton。

喜欢的东西:

    <com.google.android.material.button.MaterialButton
        android:text="BUTTON"
        app:cornerRadius="8dp"
        ../>

得到一个圆角按钮就足够了。

你可以使用一种材质按钮样式。 例如:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    .../>

从1.1.0版本开始,你还可以改变按钮的形状。只需在按钮样式中使用shapeAppearanceOverlay属性:

  <style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Button.Rounded</item>
  </style>

  <style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
  </style>

然后使用:

<com.google.android.material.button.MaterialButton
   style="@style/MyButtonStyle"
   .../>

你也可以在xml布局中应用shapeAppearanceOverlay:

<com.google.android.material.button.MaterialButton
   app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
   .../>

shapeAppearance还允许每个角有不同的形状和尺寸:

<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerFamilyTopRight">cut</item>
    <item name="cornerFamilyBottomRight">cut</item>
    <item name="cornerSizeTopLeft">32dp</item>
    <item name="cornerSizeBottomLeft">32dp</item>
</style>

在可绘制文件夹中创建一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <!-- you can use any color you want I used here gray color-->
    <solid android:color="#ABABAB"/> 
    <corners android:radius="10dp"/>
</shape>

应用此作为背景按钮,你想使角圆润。

或者你可以使用单独的半径为每个角落,如下所示

android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"

有一个app:cornerRadius属性用于普通的Button标签。

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#009688"
    android:onClick="xyz"
    android:paddingHorizontal="64dp"
    android:text="@string/login"
    app:cornerRadius="32dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/passwordCustom"
    app:layout_constraintVertical_bias="0.1" 
    />

可拉的文件夹

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="30dp"/>
    <stroke android:width="2dp" android:color="#999999"/>
</shape>

布局文件夹

<Button
    android:id="@+id/button2"
    <!-- add style to avoid square background -->
    style="@style/Widget.AppCompat.Button.Borderless"
    android:background="@drawable/corner_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

确保添加样式以避免方形背景

你也可以像下面这样使用卡片布局

  <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:cardCornerRadius="30dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Template"

                />

        </LinearLayout>

    </androidx.cardview.widget.CardView>