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


当前回答

创建可绘制的XML文件,并将按钮背景设置为此文件。

XML文件代码,例如:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--|^@^|_[ Shadows ]_|^@^|-->
    <item>
        <shape>
            <padding android:top="2dp" android:right="2dp" android:bottom="2dp" android:left="2dp" />
            <gradient android:angle="315" android:startColor="#c2c2c2" android:endColor="#c0c0c0"/>
            <corners android:radius="3dp" />
        </shape>
    </item>
    <!--|^@^|_[ Background ]_|^@^|-->
    <item>
        <shape>
            <gradient android:angle="135" android:startColor="#f7f7f7" android:endColor="#fbfcfc"/>
            <corners android:radius="3dp" />
        </shape>
    </item>
</layer-list>

其他回答

在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>

如果您使用的是矢量可绘制对象,那么您只需要在可绘制对象定义中指定一个<corners>元素。我在一篇博客文章中提到了这一点。

如果你使用位图/ 9-patch绘制,那么你需要在位图图像中创建透明的角落。

如果你想要这样的东西

这是代码。

1.在你的可绘制文件夹中创建一个xml文件,如mybutton.xml,并粘贴以下标记:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92"  />            
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <solid android:color="#58857e"/>       
        </shape>
    </item>  
    <item >
       <shape android:shape="rectangle"  >
            <corners android:radius="3dip" />
            <stroke android:width="1dip" android:color="#5e7974" />
            <gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />            
       </shape>
    </item>
</selector>

2.现在使用这个drawable作为视图的背景。如果视图是按钮,那么就像这样:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textColor="#ffffff"
    android:background="@drawable/mybutton"
    android:text="Buttons" />

图像

<androidx.cardview.widget.CardView
        android:id="@+id/add_coment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="15dp"
        android:clickable="true"
        android:foreground="?selectableItemBackground"
        android:outlineAmbientShadowColor="@color/blue_shadow_outline"
        app:cardCornerRadius="25dp"
        app:layout_constraintBottom_toBottomOf="@+id/coment"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/coment">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/btn_style2"
            android:fontFamily="@font/ar1"
            android:paddingHorizontal="10dp"
            android:text="text "
            android:textColor="@color/text_color"
            android:textSize="17dp" />
    </androidx.cardview.widget.CardView>

带有图标的样式按钮

   <Button
        android:id="@+id/buttonVisaProgress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:background="@drawable/shape"
        android:onClick="visaProgress"
        android:drawableTop="@drawable/ic_1468863158_double_loop"
        android:padding="10dp"
        android:text="Visa Progress"
        android:textColor="@android:color/white" />

shape.xml

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="14dp" />
<gradient
    android:angle="45"
    android:centerColor="#1FA8D1"
    android:centerX="35%"
    android:endColor="#060d96"
    android:startColor="#0e7e1d"
    android:type="linear" />
<padding
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp" />
<size
    android:width="270dp"
    android:height="60dp" />
<stroke
    android:width="3dp"
    android:color="#000000" />