如何自定义EditText样式

来源:互联网 发布:淘宝usb摄像头 编辑:程序博客网 时间:2024/06/11 07:51

通常系统UI组件无法满足项目需要,这时候就需要我们自定义UI组件了。

下面来看下如何自定义EditText样式。

代码实例:

main.xml

view source
print?
01<?xmlversion="1.0"encoding="utf-8"?>
02<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03    android:layout_width="fill_parent"
04    android:layout_height="fill_parent"
05    android:orientation="vertical">
06 
07    <TextView
08        android:layout_width="fill_parent"
09        android:layout_height="wrap_content"
10        android:text="@string/hello"/>
11    <EditText
12        android:layout_width="fill_parent"
13        android:layout_height="wrap_content"
14        style="@style/my_style"
15        android:hint="请输入内容"
16        />
17    <Button
18        android:layout_width="wrap_content"
19        android:layout_height="wrap_content"
20        android:text="Button"
21        />
22</LinearLayout>

mytext.xml

view source
print?
1<?xmlversion="1.0"encoding="utf-8"?>
2<selectorxmlns:android="http://schemas.android.com/apk/res/android">
3    <itemandroid:state_focused="true"android:drawable="@drawable/p1"></item>
4    <itemandroid:drawable="@drawable/p2"></item>
5</selector>

style.xml

view source
print?
1<?xmlversion="1.0"encoding="utf-8"?>
2<resources>
3    <stylename="my_style"parent="@android:style/Widget.EditText">
4        <itemname = "android:background">@drawable/mytext</item>
5    </style>
6</resources>

效果截图:

android自定义edittext