attr & style

2016-10-19  本文已影响0人  乐此不痞

attr和styleable的关系

首先要明确一点,attr不依赖于styleable,styleable只是为了方便attr的使用。自定义属性完全可以不放到styleable里面。

去获取这个属性时,必须调用如下代码:

int[] custom_attrs = {R.attr.custom_attr1,R.attr.custom_attr2};
TypedArray typedArray = context.obtainStyledAttributes(set, custom_attrs);

通过定义一个declare-styleable比如名字为custom_attrs,可以在R文件里自动生成一个int[]数组,可以直接用R.styleable.custom_attrs替换上面的custom_attrs

obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

attrs

int[],每个方法中都有的参数,就是告诉系统需要获取那些属性的值。

set

表示从layout文件中直接为这个View添加的属性的集合,注意这里面的属性必然是通过xml配置添加的,也就是由LayoutInflater加载进来的布局或者View才有这个属性集。

自己定义View的时候至少要重写(Context context, AttributeSet set)构造器,因为不重写将无法获取到layout中配置的属性!因为这样LayoutInflater在inflater布局时会通过反射去调用View(Context, AttributeSet)构造器。set 中实际上又有两种数据来源,当然最后都会包含在set中。一种是用android:layout_width="wrap_content"这类直接指定的,还有一种是通过style="@style/somestyle"这样指定的。

defStyleAttr

是自定义一个可以在Theme中配置样式的关键,如果想通过在系统主题里面设置一个样式,修改所有TextView的样式,你一般会这么做:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textViewStyle">@style/textviewstyle</item>
</style>

<style name="textviewstyle" parent="android:style/Widget.TextView">
<!--指定一些属性-->
</style>

首先android:textViewStyle其实就是一个普通的在资源文件中定义的属性attr,它的format="reference"。那TextView是怎么得知我们自己定义的textviewstyle
的呢?这其实就是defStyleAttr的应用场景:定义Theme可配置样式。

public TextView(Context context, AttributeSet attrs) {
 //指定属性textViewStyle为defStyleAttr,然后系统会去搜索Theme中为这个
 //属性配置的style
 this(context, attrs, com.android.internal.R.attr.textViewStyle);
}
public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
  this(context, attrs, defStyleAttr, 0);
  //最终调用到View的第四个构造器时,调用了obtainStyledAttributes
  TypedArray a = theme.obtainStyledAttributes(attrs,
          com.android.internal.R.styleable.TextViewAppearance,
          defStyleAttr, defStyleRes);
}

defStyleRes

resId:直接从资源文件中定义的某个样式中读取。
defStyleAtrtr为0或Theme中没有配置defStyleAtrtr时,defStyleRes才起作用

NULL

另外可以直接在Theme中指定属性的值,NULL表示直接从Theme中读取属性。

优先级:

set>defStyleAttr(主题可配置样式)>defStyleRes(默认样式)>NULL(主题中直接指定)

上一篇 下一篇

猜你喜欢

热点阅读