Android 自定義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="" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM