自定义android控件EditText边框背景

在我们进行Android应用界面设计和时候,为了界面风格的统一,我们需要对一些控件进行自定义。比如我们的应用采用的蓝色风格,但是android的EditText控制获得焦点后显示的却是黄色的边框背景。那么如何让EditText在获得焦点的时候显示的是我们自定义的蓝色的背景呢?

首先准备两张图片,一张是EditText获得焦点后的边框背景,一张是没有获得焦点时的背景,注意制作成9.png样式的图片,然后在drawable里添加一个selector_edittext_bg.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/edit_pressed" android:state_focused="true"/>
    <item android:drawable="@drawable/edit_normal"/>

</selector>

然后在values文件夹下新建一个style.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="my_edittext_style" parent="@android:style/Widget.EditText">
        <item name="android:background">@drawable/selector_edittext_bg</item>
    </style>

</resources>

最后在EditTex上使用我们新建的样式就可以了:

<EditText
            android:id="@+id/v_value"
            style="@style/my_edittext_style"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/edit_key"
            android:imeOptions="actionDone"
            android:inputType="" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章