您的位置:首页 > 移动开发

Android ContextThemeWrapper解析

2015-03-29 11:33 357 查看
转载请注明出处:Android ContextThemeWrapper解析

网上关于ContextThemeWrapper的说明比较少,这次也是遇到了才拿出来说一下的。在Chris Banes的Theme vs Style这篇文章中提到了ContextThemeWrapper的用法,最重要的就一句话:

It wraps an existing Context (say your Activity), and then overlaysa
new theme on top of that Context’s theme.

如果你能理解这句话,那么接下来的内容也就不需要看了。如果不理解的我们通过一个简单的例子来说明。

如果你还不了解关于自定义View属性的相关知识,建议先阅读下面的两篇文章,熟悉的忽略之:

Android 自定义View 之 自定义View属性

Android View构造方法第三参数使用方法详解

首先自定义一个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);
//		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, R.style.default_view_style);

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中拿到的属性也就是新的值了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: