1. 程式人生 > >自定義edittext邊框

自定義edittext邊框

EditText 的自帶屬性裡沒有設定邊框顏色的 

有倆種方式可以達到效果:一種是網上比較推崇的用圖作背景,另一種則是自繪 

圖作背景的: 
  首先重新定義一個style。在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/my_edittext</item>
            </style>
         </resources>
接下來在drawable裡新增my_edittext.xml:內容如下 
 <?xml version="1.0" encoding="utf-8"?>
          <selector xmlns:android="http://schemas.android.com/apk/res/android">
               <item android:state_focused="true" android:drawable="@drawable/editbox_focus" />
               <item android:drawable="@drawable/editbox_normal" />
         </selector> 

     其中editbox_normal為正常情況下的編輯框圖片,editbox_focus為選中下的編輯框圖片 

定義好了這兩個檔案之後就可以用以下方式使用: 
<EditText
      style="@style/my_edittext_style"
      android:text="My EditText"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content">
    </EditText>