查看特定View的默认属性值

时间:2023-03-08 22:06:03

  当我在分析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。