天天看點

android EditText自定義樣式

1.去掉邊框

android EditText自定義樣式

edittext的background屬性設定為@null就搞定了:android:background="@null"

style屬性倒是可加可不加

附原文:

@slumbermachine, that's a great observation! but, it seems that there is more to making a textview editable than just setting android:editable="true". it has to do with the "input method" - what ever that is - and that is where the real difference between textview

and edittext lies. textview was designed with an edittext in mind, that's for sure. one would have to look at the edittext source code and probably edittext style to see what's really going on there. documentation is simply not enough.

i have asked the same question back at android-developers group, and got a satisfactory answer. this is what you have to do:

xml:

<edittext android:id="@+id/title" android:layout_width="fill_parent"

     style="?android:attr/textviewstyle"

     android:background="@null" android:textcolor="@null"/>

instead of style="?android:attr/textviewstyle" you can also write style="@android:style/widget.textview",

don't ask me why and what it means.

2.android edittext 改變邊框顔色

第一步:為了更好的比較,準備兩個一模一樣的edittext(當activity啟動時,焦點會在第一個edittext上,如果你不希望這樣隻需要寫一個高度和寬帶為0的edittext即可避免,這裡就不這麼做了),代碼如下:

[html] view

plaincopy

<edittext   

    android:layout_width="fill_parent"  

        android:layout_height="36dip"  

        android:background="@drawable/bg_edittext"  

        android:padding="5dip"  

    android:layout_margin="36dip"  

    android:textcolorhint="#aaaaaa"  

    android:textsize="15dip"  

    android:singleline="true"  

    android:hint="請輸入..."  

/>  

接下來建立三個xml檔案,分别為輸入框未獲得焦點時的背景,輸入框獲得焦點時的背景,selector背景選擇器(這裡能獲得輸入框什麼時候獲得和失去焦點),代碼如下:

bg_edittext_normal.xml(未獲得焦點時)

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

<shape xmlns:android="http://schemas.android.com/apk/res/android">   

    <solid android:color="#ffffff" />   

    <corners android:radius="3dip"/>  

    <stroke    

        android:width="1dip"    

        android:color="#bdc7d8" />   

</shape>  

bg_edittext_focused.xml(獲得焦點時)

        android:color="#728ea3" />   

bg_edittext.xml(selector選擇器,這方面資料網上很多)

<selector xmlns:android="http://schemas.android.com/apk/res/android">  

        <item android:state_window_focused="false" android:drawable="@drawable/contact_edit_edittext_normal" />  

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

</selector>  

這樣就ok了,效果圖如下:

android EditText自定義樣式

第二個輸入框邊框變為深色,是不是這樣更友好點。

繼續閱讀