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


当前回答

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

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

其他回答

在Android Studio中,有一个拐角半径属性,让你使用输入,如10dp

创建文件myButton.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorButton"/>
    <corners android:radius="10dp"/>
</shape>

添加到按钮

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/myButton"/>

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

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

在可绘制文件夹中创建shape.xml

像shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <stroke android:width="2dp"
    android:color="#FFFFFF"/>
  <gradient 
    android:angle="225"
    android:startColor="#DD2ECCFA"
    android:endColor="#DD000000"/>
<corners
    android:bottomLeftRadius="7dp"
    android:bottomRightRadius="7dp"
    android:topLeftRadius="7dp"
   android:topRightRadius="7dp" />
</shape>

在myactivity。xml中

你可以使用

<Button
    android:id="@+id/btn_Shap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/Shape"
    android:background="@drawable/shape"/>

我发现的简单方法是在可绘制文件夹中制作一个新的xml文件,然后将按钮背景指向该xml文件。下面是我使用的代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<solid android:color="#ff8100"/>
<corners android:radius="5dp"/>

</shape>