开发者

Android - how to re-inflate View hierarchy

I'm having problems making my Activity respond to a theme change (i.e. Theme_Dark change to Theme_Light).

In the Activity this code below works fine, and the theme is changed when the Activity is created (method getPreferenceTheme() just开发者_如何学编程 gets the theme preference value that was set via a PreferenceActivity).

protected void onCreate(Bundle savedInstanceState) {
    setTheme(getPreferenceTheme());
    super.onCreate(savedInstanceState);
    setContentView(R.layout.controls);
}

But how can I dynamically change the theme ? So after I change the theme in a PreferenceActivity and return to the main Activity how can I get it to change?

I know that I can re-start the Activity to do this (calling onCreate() again), but I didn't want to do this and have heard that it is possible to "re-inflate the view hierarchy" in onResume() - how do I do this ?

I tried the following (a stab in the dark) but with no joy.

protected void onResume() {
    super.onResume();    
    LayoutInflater inflater = LayoutInflater.from (this);
    View v = inflater.inflate (R.layout.controls, null);
    setTheme(getPreferenceTheme());
    setContentView(v);
}

Any help much appreciated, M.


Try the "recreate" Activity method.

http://developer.android.com/reference/android/app/Activity.html#recreate()

Call it after you have set the new theme.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜