當我在分析focus、touch事件處理代碼時發現,有些屬性對代碼的邏輯有非常重要的影響,比如clickable、focusable
這些屬性。這時我們自然而然的想到,那么這些屬性的默認值是什么呢?在工作中我也很多次有同樣的疑問。當初我也不是
很清楚,基本都是手動在xml里面設置下。相信和我一樣的人還有很多,今天我就告訴大家怎么通過Android源碼來快速查看這些默認值。
比如我們經常用到的TextView,感覺上來說,它應該是不能點擊的,也就是clickable默認應該是false。接下來,我們通過
源碼來看看到底是不是這樣,先來看其2個參數的ctor,代碼如下:
public TextView(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.textViewStyle); }
通過代碼我們看到實際上調用了3個參數的版本,且第3個參數給的是com.android.internal.R.attr.textViewStyle。接着我們去
系統的attrs.xml看看,找到了類似這樣的代碼:
<!-- Default TextView style. --> <attr name="textViewStyle" format="reference" />
這些屬性具體的值是在系統的themes.xml文件中設置的,具體見:
<style name="Theme"> <item name="textViewStyle">@android:style/Widget.TextView</item> </style> <style name="Theme.Holo"> <item name="textViewStyle">@android:style/Widget.Holo.TextView</item> </style> <style name="Theme.Holo.Light"> <item name="textViewStyle">@android:style/Widget.Holo.Light.TextView</item> </style>
然后我們再去styles.xml文件中看看,這些style是如何設置的,代碼如下:
<style name="Widget.TextView"> <item name="android:textAppearance">?android:attr/textAppearanceSmall</item> <item name="android:textSelectHandleLeft">?android:attr/textSelectHandleLeft</item> <item name="android:textSelectHandleRight">?android:attr/textSelectHandleRight</item> <item name="android:textSelectHandle">?android:attr/textSelectHandle</item> <item name="android:textEditPasteWindowLayout">?android:attr/textEditPasteWindowLayout</item> <item name="android:textEditNoPasteWindowLayout">?android:attr/textEditNoPasteWindowLayout</item> <item name="android:textEditSidePasteWindowLayout">?android:attr/textEditSidePasteWindowLayout</item> <item name="android:textEditSideNoPasteWindowLayout">?android:attr/textEditSideNoPasteWindowLayout</item> <item name="android:textEditSuggestionItemLayout">?android:attr/textEditSuggestionItemLayout</item> <item name="android:textCursorDrawable">?android:attr/textCursorDrawable</item> </style>
這些就是TextView默認的屬性值,沒設置的clickable、focusable就是false。同樣的方式我們看看TextView的子類Button是如何設置的:
<style name="Widget.Button"> <item name="android:background">@android:drawable/btn_default</item> <item name="android:focusable">true</item> <item name="android:clickable">true</item> <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item> <item name="android:textColor">@android:color/primary_text_light</item> <item name="android:gravity">center_vertical|center_horizontal</item> </style>
我們可以看到Button默認的focusable和clickable都是true,這里也設置了其他幾個常見的屬性如background、textColor、gravity等。
最后我們看下EditText的,代碼如下:
<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:attr/editTextBackground</item> <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item> <item name="android:textColor">?android:attr/editTextColor</item> <item name="android:gravity">center_vertical</item> </style>
相對Button來說,其focusableInTouchMode也默認為true,這樣保證即使在touch mode下,EditText也能獲得focus,用戶能夠分清楚
正在接受輸入的控件是哪個。
這篇文章算是開發過程中的小技巧,通過這里介紹的方法你就可以很輕松的知道任意一個Android view的默認屬性值了。最后推薦一篇
園友的關於自定義樣式&屬性值獲取的文章:http://www.cnblogs.com/angeldevil/p/3479431.html。