TypedArray是存储资源数组的容器,他可以通过obtaiStyledAttributes()方法创建出来。
创建完后请注意调用recycle()方法把它释放。避免重新创建时产生错误。
Context提供四个方法进行创建,具体如下
*检索当前主题的属性,attrs:属性名数组
1.obtainStyledAttributes(int[] attrs)
*检索当前style的属性,resid:样式id,attrs:属性名数组
2.obtainStyledAttributes(int resid, int[] attrs)
*检索当前属性值的基本集的属性,set:属性基本集,attrs:属性名数组。
属性基本集:即在xml中直接设置的属性,如:android:text=”” 或 xxx:yyy=”“,自定义xxx为命名空间,yyy为属性值。
3.obtainStyledAttributes(AttributeSet set, int[] attrs)
*检索当前属性值的基本集、当前主题、当前style的属性,set:属性基本集,attrs:属性名数组。
4.obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
自定View须知
public class MyView extends View {
public MyView(Context context){
super(context);
//通过java代码new的时候调用
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//通过xml创建实例时调用,后面两个构造器由者两个间接调用
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
具体操作
1.TypedArray ta = context.obtainStyledAttributes(new int[]{R.attr.colorAccent});
ta.getColor(R.styled.colorAccent,Color.WHITE);或ta.getColor(0);//此时只有一个属性知道属性在R文件排列顺序可以直接使用索引值
R.attr.colorAccent:为theme里的属性名,这里直接使用默认APPTheme属性里的colorAccent
R.styled.colorAccent:在attrs文件构建colorAccent属性,不指定format表示该属性为系统属性。
2.TypedArray ta = context.obtainStyledAttributes(R.style.mystyle,new int[]{R.attr.XXX});
ta.getColor(R.styled.XXX,Color.WHITE);或ta.getColor(0);//此时只有一个属性知道属性在R文件排列顺序可以直接使用索引值
R.style.mystyle:在xml中创建MyView并引用Style
R.attr.XXX:为style里的属性名
R.styled.XXX:在attrs文件构建colorAccent属性,不指定format
在这里你能发现,要从ta里获得当前指定的Theme,style的属性值,要在attrs文件定义,如果是系统已有不需指定format类型。
3.TypedArray ta = context.obtainStyledAttributes(set, R.styled.myattrs);
ta.getColor(R.styled.XXX,Color.WHITE);或ta.getColor(0);
set:基本属性集
R.styled.myattrs:在attrs定义的属性集名。(根元素declare-styleable)
R.styled.XXX:在attrs文件构建XXX属性,不指定format表示该属性为系统属性。
4.TypedArray ta = context.obtainStyledAttributes(set, R.styled.myattrs, new int[]{R.attr.colorAccent}, R.style.mystyle);
ta.getColor(R.styled.XXX,Color.WHITE);或ta.getColor(0);
set:基本属性集
R.styled.myattrs:在attrs定义的属性集名。(根元素declare-styleable)
R.attr.colorAccent:为theme里的属性名,这里直接使用默认APPTheme属性里的colorAccent
R.styled.XXX:在attrs文件构建XXX属性,不指定format表示该属性为系统属性。
R.style.mystyle:在xml中创建MyView并引用Style
其实第四个方法是前三个的结合版..
TypedArray是存储资源数组的容器
属性集+attrs属性集,style,theme里的属性,相当于表明你想要什么属性值存储在里面。
要得到属性值,通过attrs属性集,系统的也要在定义,只是不需指定format。
具体代码
MyView:
<com.example.brazen_zz.typedarray.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/mystyle"
></com.example.brazen_zz.typedarray.MyView>
mystyle:
<style name="mystyle">
<item name="mytest1">aaaa</item>
</style>
AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
myattrs:
<declare-styleable name="myattrs">
<attr name="mytest1" format="string"/>
<attr name="colorAccent"></attr>
</declare-styleable>