我想在Android程序中创建圆角按钮。我已经看过How to create EditText with rounded corners?了
我想要实现的是:
发布于 2012-02-18 05:29:39
你可以做一个圆角按钮,而不需要求助于ImageView。
后台选择器资源button_background.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states
-->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_unfocused" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_unfocused" />
<!-- Focused states
-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_focus" />
<!-- Pressed
-->
<item android:state_pressed="true" android:drawable="@drawable/button_press" />
</selector>
对于每个州,都有一个可提取的资源,例如button_press.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF404040" />
<corners android:radius="6dp" />
<gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" />
</shape>
注意corners
元素,它会让你变成圆角!
然后在按钮上设置可绘制的背景:
android:background="@drawable/button_background"
EDIT (9/2018):同样的技术也可以用来创建圆形按钮。圆实际上是一个半径大小设置为正方形边的1/2的正方形按钮
此外,在上面的示例中,stroke
和gradient
不是必需的元素,它们只是示例和查看圆角形状的方法
发布于 2012-12-14 11:50:40
如果你在Android中需要一个圆角按钮,那么创建一个XML文件"RoundShapeBtn.xml“作为drawable。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">
<solid android:color="#6E6E6E"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
将此代码添加到按钮代码中:
android:background="@drawable/RoundShapeBtn"
发布于 2015-02-13 15:27:58
在android的可绘制文件夹中创建xml文件,如下所示:
rounded_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp"/> // if you want clear round shape then make radius size is half of your button`s height.
<solid android:color="#EEFFFFFF"/> // Button Colour
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp"/>
</shape>
现在
这个xml文件作为你的按钮背景。
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/rounded_button"
android:text="@string/button_text"
android:textColor="@color/black"/>
https://stackoverflow.com/questions/9334618
复制相似问题