自定義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="" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章