在android中创建带有圆角的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12771367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Button with rounded corners in android
提问by coderplus
I'm trying to create a button which looks as in the image above. Initially my idea was to create a 9 patch and set it as the button background. But since, this is a plain button, i think we can somehow draw this without using any images.
我正在尝试创建一个如上图所示的按钮。最初我的想法是创建一个 9 补丁并将其设置为按钮背景。但由于这是一个普通按钮,我认为我们可以在不使用任何图像的情况下以某种方式绘制它。
The button background color is #0c0c0c The border color is #1a1a1a The text color is #cccccc
按钮背景色为#0c0c0c 边框色为#1a1a1a 文字色为#cccccc
I found a similar question on SO but that creates a gradient -
Android - border for button
我在 SO 上发现了一个类似的问题,但这会创建一个渐变 -
Android - 按钮边框
回答by Sam
The Android Developer's Guide has a detailed guide on this: Shape Drawbables.
Android 开发人员指南对此有详细指南:Shape Drawbables。
You could also simply remove the gradient
element from the link you provided:
您也可以简单地gradient
从您提供的链接中删除该元素:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#1a1a1a" />
</shape>
回答by Raghunandan
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginBottom="56dp"
android:text="Button"
android:textColor="#FF0F13"
android:background="@drawable/bbkg"/>//create bbkg.xml in drawable folder
bbkg.xml//button background
bbkg.xml//按钮背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
normal.xml //button background normal state
normal.xml //按钮背景正常状态
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#10EB0A"/>
<stroke android:width="3dp"
android:color="#0FECFF" />
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
pressed.xml //button background pressed state
Pressed.xml //按钮背景按下状态
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF1A47"/>
<stroke android:width="3dp"
android:color="#0FECFF"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
回答by Andriya
If you want something like this
如果你想要这样的东西
Button preview
按钮预览
here is the code.
这是代码。
1.Create a xml file in your drawable folder like mybutton.xml and paste the following markup:
1.在你的 drawable 文件夹中创建一个 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.Now use this drawable for the background of your view. If the view is button then something like this:
2.现在使用此可绘制对象作为视图的背景。如果视图是按钮,则如下所示:
<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" />