首先自定义一个View
public class CustomView extends View {
static final String LOG_TAG = "CustomView";
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.customViewStyle);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);
Log.d(LOG_TAG, "array length = " + array.getIndexCount());
Log.d(LOG_TAG, "attr1 => " + array.getString(R.styleable.CustomView_attr1));
Log.d(LOG_TAG, "attr2 => " + array.getString(R.styleable.CustomView_attr2));
Log.d(LOG_TAG, "attr3 => " + array.getString(R.styleable.CustomView_attr3));
Log.d(LOG_TAG, "attr4 => " + array.getString(R.styleable.CustomView_attr4));
Log.d(LOG_TAG, "attr5 => " + array.getString(R.styleable.CustomView_attr5));
Log.d(LOG_TAG, "attr6 => " + array.getString(R.styleable.CustomView_attr6));
array.recycle();
}
}
定义好属性attrs.xml,
<resources>
<declare-styleable name="CustomView">
<attr name="attr1" format="string" />
<attr name="attr2" format="string" />
<attr name="attr3" format="string" />
<attr name="attr4" format="string" />
<attr name="attr5" format="string" />
<attr name="attr6" format="string" />
</declare-styleable>
<attr name="customViewStyle" format="reference" />
<attr name="customViewTheme" format="reference" />
</resources>
styles文件定义如下:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="customViewStyle">@style/custom_view_style</item>
</style>
<style name="custom_view_style">
<item name="attr1">attr1 from custom_view_style</item>
<item name="attr2">attr2 from custom_view_style</item>
<item name="attr3">attr3 from custom_view_style</item>
<item name="attr4">attr4 from custom_view_style</item>
<item name="attr5">attr5 from custom_view_style</item>
<item name="android:background">#ffff00ff</item>
</style>
</resources>
我们直接在Activity中通过JAVA代码的形式来创建这个View,如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new CustomView(this, null, R.attr.customViewStyle);
}
非常简单的代码,最后运行的结果如下:
因为并没有XML文件为控件设置属性,所以拿到的属性都是style中定义的。
为了讲解ContextThemeWrapper,我们在attrs.xml中添加一个属性:
<attr name="customViewTheme" format="reference" />
修改styles文件,添加如下的内容:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="customViewStyle">@style/custom_view_style</item>
<item name="customViewTheme">@style/AppTheme.CustomViewTheme</item>
</style>
<style name="AppTheme.CustomViewTheme">
<item name="customViewStyle">@style/custom_view_style_for_context_theme_wrapper</item>
</style>
<style name="custom_view_style_for_context_theme_wrapper">
<item name="attr1">attr1 from custom_view_style_for_context_theme_wrapper</item>
<item name="attr2">attr2 from custom_view_style_for_context_theme_wrapper</item>
<item name="attr3">attr3 from custom_view_style_for_context_theme_wrapper</item>
<item name="attr4">attr4 from custom_view_style_for_context_theme_wrapper</item>
<item name="attr5">attr5 from custom_view_style_for_context_theme_wrapper</item>
<item name="attr6">attr6 from custom_view_style_for_context_theme_wrapper</item>
<item name="android:background">#ffff00ff</item>
</style>
style中添加了一个customViewTheme,然后在这个style中重写定义了一个customViewStyle,然后在代码中使用它们:
TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(R.attr.customViewTheme, outValue, true);
if (outValue.resourceId != 0) {
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, outValue.resourceId);
new CustomView(wrapper, null, R.attr.customViewStyle);
}
看结果:
拿到的数据全是我们刚刚定义的。这是为什么呢?我们从上面的代码分析起。
getTheme().resolveAttribute()是从当前主题中拿到R.attr.customViewTheme所对应的那个值,因为在我们的主题中设置了,所以可以拿到值,并且拿到的是一个style的id,即R.style.AppTheme_CustomViewTheme,拿到这个style后构建一个ContextThemeWrapper,传入当前的context和这个style,那么这个ContextThemeWrapper做了什么事呢?这就要回到文章开头提到的那句话了,翻译过来基本上是:用style中的各个属性,去覆盖已有的context中的属性,覆盖之后得到的context就是改变了部分属性后的 context了,然后用这个context去构建View,View中拿到的属性也就是新的值了。