標籤:
在我們進行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="" />
Android 自訂android控制項EditText邊框背景