1.android:background 屬性
指定控件背景
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:background="@drawable/editor_selector" />
2.drawable 資源文件
配置樣式屬性
editor_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--常規演示-->
<item android:drawable="@drawable/shape_edit_normal" />
<!--得到焦點時的樣式-->
<item android:drawable="@drawable/shape_edit_focus" android:state_focused="true" />
</selector>
shape_edit_normal.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--背景色--> <solid android:color="#ff00ff" /> <!--邊框樣式--> <stroke android:width="5dp" android:color="#ffaaaaaa"/> <!--圓角樣式--> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp"/> <!--padding 邊距樣式--> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp"/> </shape>
shape_edit_focus.xml
與shape_edit_normal.xml 差別不大
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--背景色--> <solid android:color="#ff00ff" /> <stroke android:width="5dp" android:color="#ff0000aa"/> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp"/> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp"/> </shape>
3.效果如下
很丑,但是很溫柔。