Android Button 添加边框
button控件属性是没有用来指定边框色的。也就是没办法像指定背景色那样直接用background属性就行了。那要完成以下样式,该如何做呢?
- 先在res/drawable文件夹下添加一个xml文件
- xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 边框颜色值 -->
<item>
<shape>
<solid android:color="@android:color/white" />
</shape>
</item>
<!-- 主体背景颜色值,控件间的间距 -->
<item
android:bottom="3dp"
android:top="3dp"
android:left="3dp"
android:right="3dp">
<shape>
<solid android:color="@android:color/black" />
</shape>
</item>
</layer-list>
- 最后就可以在button中使用该样式上色,在background使用刚才定制的样式,有必要时改下文本颜色(textcolor)
<Button
android:text="@strings/txt_btn13"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/btn13"
android:background="@drawable/btn_styles"
android:textColor="@android:color/white"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="1" />
- 布局选择水平布局嵌套垂直布局或垂直布局嵌套水平布局即可。结果: