2011-05-19 30 views
306

我想做一个button回合的角落。有没有一种简单的方法可以在Android中实现这一点?如何使一个按钮的角落圆?

+8

苏尼尔:哥们,这是一个诚实的问题。我也搜索了很多自己,想出一个合理的答案 - 这并不简单。我期待Android SDK在Button对象本身提供这个属性... – 2011-11-21 18:25:06

+2

看看这个:http://nishantvnair.wordpress.com/2010/11/09/customize-button-in-android/ – Lavanya 2011-05-19 06:19:24

+0

检查这个out http://www.gadgetsaint.com/tips/rounded-corners-views-layouts-android/#.WPz2QVN97BI – ASP 2017-04-23 18:50:07

回答

501

如果你想是这样的

Button preview

这里是代码。

1,创建像mybutton.xml您绘制文件夹中的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.Now使用此绘制你的视图的背景。如果该视图按钮,然后是这样的:

<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" /> 
+0

它崩溃:原因:org.xmlpull.v1.XmlPullParserException:二进制XML文件行#24:标记需要'drawable'属性或子标记定义可绘制的 – Zennichimaro 2014-07-03 07:50:12

+2

我们可以通过编程来实现。 – Killer 2015-07-24 15:00:17

+0

它说元素选择器必须声明编辑:对不起,该文件是在值文件夹而不是drawable。当我转移它时,它工作。 – 2016-11-17 07:51:26

4

如果您使用的是矢量绘图,那么您只需在您的可绘制定义中指定<角落>元素。我已经在blog post中报道过这个问题。

如果您使用位图/ 9-补丁drawables,则需要在位图图像中创建具有透明度的边角。

274

像下面

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

应用以此为背景按钮创建绘制文件夹中的XML文件,你想要做的角落一轮。

或者你可以使用不同的半径每一个角落,像下面

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

你可以缩短角落到android:radius =“10dp”,这将适用于所有 – 2012-06-30 19:01:34

+20

这是不是一个完整的解决方案,因为它不支持各种按钮状态(按下,关注,默认)。为了更好的解决方案,请参阅http://stackoverflow.com/questions/9334618/roundedbutton-android – JosephL 2013-03-21 21:10:15

+3

@BenSimpson,你会看到,当你只放一条线而不是定义每一个角落半径单独。 – 2013-05-29 05:27:25

19

在绘制文件夹

创建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"/> 
8

我发现的简单方法是在可绘制文件夹中创建一个新的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> 
+0

要在自定义可绘制背景中恢复材质主题涟漪效果,请在按钮视图中添加'android:foreground =“?attr/selectableItemBackground”''。请参阅https://stackoverflow.com/questions/38327188/android-setting-background-color-of-button-loses-ripple-effect – 2018-02-13 17:58:12

10

创建可绘制文件夹rounded_btn.xml文件...

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@color/#FFFFFF"/>  

    <stroke android:width="1dp" 
     android:color="@color/#000000" 
     /> 

    <padding android:left="1dp" 
     android:top="1dp" 
     android:right="1dp" 
     android:bottom="1dp" 
     /> 

    <corners android:bottomRightRadius="5dip" android:bottomLeftRadius="5dip" 
     android:topLeftRadius="5dip" android:topRightRadius="5dip"/> 
    </shape> 

,并使用this.xml文件作为按钮的背景

<Button 
android:id="@+id/btn" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/rounded_btn" 
android:text="Test" /> 
2

风格的按钮带图标 enter image description here

<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" /> 

形状。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" /> 

2

创建文件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 
     /> 
4

此链接有你需要的所有信息。 Here

Shape.xml

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

    <solid android:color="#EAEAEA"/> 

    <corners android:bottomLeftRadius="8dip" 
       android:topRightRadius="8dip" 
       android:topLeftRadius="1dip" 
       android:bottomRightRadius="1dip" 
       /> 
</shape> 

和main.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

    <TextView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Hello Android from NetBeans"/> 

    <Button android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Nishant Nair" 
      android:padding="5dip" 
      android:layout_gravity="center" 
      android:background="@drawable/button_shape" 
      /> 
</LinearLayout> 

这应该给你你想要的结果。

祝你好运

9

这很简单。创建一个如下所示的XML文件。将其设置为按钮的背景。如果需要更多按钮的曲线,请将radius属性更改为您的愿望。

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"/>