展会信息港展会大全

Activity如何实现主题变化
来源:互联网   发布日期:2016-01-14 10:02:34   浏览:1609次  

导读:要想回答这一问题,我们需要先从Activity的基类入手来看Context类,该类是一个抽象类,为访问应用程序的环境信息提供了全局的接口,通过它可以访问到应用程序的资源,类型,以及运行中的Activitys,正在广播和接......

要想回答这一问题,我们需要先从Activity的基类入手

来看Context类,该类是一个抽象类,为访问应用程序的环境信息提供了全局的接口,通过它可以访问到应用程序的资源,类型,以及运行中的Activitys,正在广播和接收中的Intents等。

/** Return a Resources instance for your application's package. */

public abstract Resources getResources();

/**

* Set the base theme for this context.Note that this should be called

* before any views are instantiated in the Context (for example before

* calling {@link android.app.Activity#setContentView} or

* {@link android.view.LayoutInflater#inflate}).

*

* @param resid The style resource describing the theme.

*/

public abstract void setTheme(int resid);

/**

* Return the Theme object associated with this Context.

*/

public abstract Resources.Theme getTheme();

既然Context类是一个抽象类,没有提供具体的实现,我们就循迹来看它的子类ContextWrapper类,该类是一个具本类,实现了Context的接口,但实现方式很简单,只是简单的委托给其他的Context对象。例如:

@Override

public Resources getResources()

{

return mBase.getResources();

}

@Override

public void setTheme(int resid) {

mBase.setTheme(resid);

}

@Override

public Resources.Theme getTheme() {

return mBase.getTheme();

}

继续来看ContextWrapper的子类ContextThemeWrapper,该类是针对Context主题部分的包装类,其主要功能是主题变换,在GetTheme()中通过initializeTheme()的调用将主题启用。

protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {

theme.applyStyle(resid, true);

}

private void initializeTheme() {

final boolean first = mTheme == null;

if (first) {

mTheme = getResources().newTheme();

Resources.Theme theme = mBase.getTheme();

if (theme != null) {

mTheme.setTo(theme);

}

}

onApplyThemeResource(mTheme, mThemeResource, first);

}

至此,Activity派生于ContextThemeWrapper,就具备了变换主题的能力了。

欢迎转载,请注明出处,谢谢

原文链接:http://blog.csdn.net/caowenbin/article/details/5868648

赞助本站

人工智能实验室

相关热词: Activity实现主题变化

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港