Android去掉EditText控件周圍橙色高亮區域【屬性文件位置】

Android_去掉EditText控件周圍橙色高亮區域

先來看看默認的EditText控件效果:

佈局就是一個Activity裏就放了一個EditText控件,可以看到四周有橙色的高亮區域

處理後的效果:

 

接下來簡單描述下處理過程:

1,查看EditText這個類的源碼

    public EditText(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.editTextStyle);
    }

在其構造方法裏默認使用了系統定義的風格 com.android.internal.R.attr.editTextStyle

 

2,找到這個定義的屬性:

在android源碼的\frameworks\base\core\res\res\values路徑下找到attrs.xml文件,打開找到:

        <!-- Default EditText style. -->
        <attr name="editTextStyle" format="reference" />

找到了源碼中引用的系統定義的風格,但這裡啥都木有(一開始找到這,不知道format="reference"代表啥,在此感謝一個神奇的網站:stackoverflow)

接着找到這個<attr .... >的根節點

<!--根元素-->
<declare-styleable name="Theme">

<!-- n個attr及其他標籤-->

<!-- Default EditText style. -->
        <attr name="editTextStyle" format="reference" />

......

</.....>

根節點的name為"Theme",接着就是要找到name="Theme"的style

(也就是說format="reference"表示EditText控件所默認使用的com.android.internal.R.attr.editTextStyle資源定義在name="Theme"中,即後者被前者引用)

 

3, 找到name="Theme"的style:

attrs.xml所在路徑下有很多系統定義的資源,打開themes.xml, 在name="Theme"的style節點下可以看到:

<item name="editTextStyle">@android:style/Widget.EditText</item>

 

4,ok, 繼續,打開同路徑下styles.xml文件,內牛滿面,引用了半天,終於找到了

    <style name="Widget.EditText">
        <item name="android:focusable">true</item>
        <item name="android:focusableInTouchMode">true</item>
        <item name="android:clickable">true</item>
        <item name="android:background">@android:drawable/edit_text</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
        <item name="android:textColor">@android:color/primary_text_light</item>
        <item name="android:gravity">center_vertical</item>
    </style>

我們要去掉高亮區域,其實也就是換背景(後面就清楚的看到),與之相關的屬性自然是

<item name="android:background">@android:drawable/edit_text</item>

 

5,回到frameworks\base\core\res\res目錄,在drawable文件夾下找到edit_text.xml文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/textfield_default" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/textfield_disabled" />
    <item android:state_pressed="true" android:drawable="@drawable/textfield_pressed" />
    <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_selected" />
    <item android:state_enabled="true" android:drawable="@drawable/textfield_default" />
    <item android:state_focused="true" android:drawable="@drawable/textfield_disabled_selected" />
    <item android:drawable="@drawable/textfield_disabled" />
</selector>

 

這下應該很清楚了,在selector中定義了N多item表示N多狀態,看後面android:drawable="...."所引用的圖片資源,有好幾種

這裡我先貼兩張系統內部使用的圖片:

@drawable/textfield_default

@drawable/textfield_selected

@drawable/textfield_disabled_selected

之所以這麼小,因為是.9圖片,俗稱9妹

一目瞭然,所以上面說橙色高亮區域是有點不準確的,因為其實就是根據狀態來切換背景圖片而已

哪有什麼高亮,橙色本來就是圖片的一部分(哎,語文太差, 描述不好)

 

這個東西挖到這,應該是挖到祖墳了,只要重寫一個背景xxx.xml供android:background屬性調用即可:

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

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章