bg_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<solid android:color="#0000"></solid>
<stroke android:width="4px" android:color="#f00"></stroke>
<corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"></corners>
</shape>
最後再把TextView的android:background屬性設置成自定義的xml樣式文件就可以了:
android:background="@drawable/bg_border"
另外還可以設置漸變色、填充色等,如下:
gradient -- 顏色漸變
startcolor 起點顏色
endcolor 終點顏色
android:angle 角度 0是從左到右,90是從下到上
solid -- 填充
stroke -- 描邊
corners -- 圓角
padding -- 內容離邊界的距離
同理,設置Button在不同狀態下的背景,也是使用在Drawable文件夾下添加xml文件,不過資源類型改爲selector。
selector.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/temp1" />
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@drawable/temp2" />
<item android:state_focused="true" android:drawable="@drawable/temp3" />
<item android:state_focused="false" android:drawable="@drawable/temp4" />
</selector>
然後在button中設置如下:
<Button
android:background="@drawable/selector"/>