每个控件都有很多属性 而对于一些属性会有其默认值 而这些默认值是哪里来的?
我们会想到style或者theme 可往往我们使用TextView或者一些常用的控件的时候并没有声明 style属性 或者theme属性啊
下面以最常用的TextView来进行分析
我们知道 开发中缩写的xml 布局文件 最后都会被解析成为一个对象
势必会调用构造方法来创建对象
下面我们来看看TextView的构造方法
<span style="font-size:14px;"> public TextView(Context context) {
this(context, null);
}
public TextView(Context context,
AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}
@SuppressWarnings("deprecation")
public TextView(Context context,
AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
.....
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.TextView, defStyle, 0);
.......
}</span>
TextView 共有3个构造方法 android提供的控件 都会有3个构造方法
第一个构造方法 需要我们传入一个Context对象 一般用于在代码中创建对象
而第二三个构造方法 则是在xml解析成对象时调用
当控件没有指定style时调用第二个构造方法
指定了style时调用第三个
对于TextView 我们一般不指定style 此时就会调用第二个构造方法
<span style="font-size:14px;"> public TextView(Context context,
AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}
</span>
可以看到这里调用了第三个构造方法
<span style="font-size:14px;">public TextView(Context context,
AttributeSet attrs,
int defStyle) {}</span>
分析一下参数:
context 是上下文环境 由系统提供
attrs 是解析xml文件中 控件的属性(id,layout_height等)得来的 可以视为一个容器
defStyle 是第二个构造函数传进来的
com.android.internal.R.attr.textViewStyle
可以看出这是一个id引用对象 在系统attr.xml文件中定义
<span style="font-size:14px;"> <!-- Default TextView style. -->
<attr name="textViewStyle" format="reference" /></span>
由此可知当我们没有为控件指定style时 会使用一个默认style
那么这个默认style从哪来的啊 我们也并没有为这个textViewStyle设定值啊 ?
答案是 在activity的theme中指定了textViewStyle
<span style="font-size:14px;"> <style name="Theme">
<item name="textViewStyle">@android:style/Widget.TextView</item></span>
定义在了Theme中 而Theme是所有theme的基类 所以无论activity 的theme是什么 都可以找到textViewStyle
@android:style/Widget.TextView
这个表示引用系统style资源Widget.TextView
<span style="font-size:14px;"> <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>
</style></span>
以上就是Textview的默认属性了
总结:
控件的默认值在style中指定
当不指定控件style 会使用默认的style
而默认的style的值则在theme中指定
下面分析
<span style="font-size:14px;">TypedArray a=context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyle, 0);</span>
这个obtainStyledAttributes可以理解为使用attrs 和defStyle 对com.android.internal.R.styleable.TextView中的属性进行解析
我们知道 attrs是xml布局文件中控件指定的属性值 而defStyle 是style中指定属性值
而com.android.internal.R.styleable.TextView 是在scheme中即attrs.xml中定义的属性
所以把attrs 和defStyle的值匹配到com.android.internal.R.styleable.TextView的属性上
<span style="font-size:14px;"> mThumbDrawable = a.getDrawable(R.styleable.Switch_thumb);
mTrackDrawable = a.getDrawable(R.styleable.Switch_track);
mTextOn = a.getText(R.styleable.Switch_textOn);
mTextOff = a.getText(R.styleable.Switch_textOff);</span>
最后通过以上代码得到属性值