从首选项更改布局

时间:2011-12-20 17:47:48

标签: android

我有两个不同的布局。应用程序从第一个开始,但我希望用户将其更改为我的第二个布局,如果他更喜欢它。我怎么能这样做?谢谢

3 个答案:

答案 0 :(得分:1)

使用此功能,只需从您的SharedPreferences

中拉取pref
if (pref == 1){
setContentView(layout1)
} else{
setContentView(layout2)
}

或者你可以把它变成一个布尔值,如上所述

答案 1 :(得分:0)

有几种方法,更容易创建一个布尔首选项(如果你想处理两个以上的布局,则是一个整数),如果它设置为true,那么你加载一个特定的布局,如果没有你加载另一个:

if (prefs.getBoolean("firstLayout", true))
    setContentView(R.layout.first);
else
    setContentView(R.layout.second);

答案 2 :(得分:0)

只需在SharedPreferences.Let中保留一个值,它就是userSelected。如果用户选择该布局,则在sharedPreferences中将userSelected设置为true。

现在,在setContentView或Layout inflayout之前检查SharedPreference的值。如果是true,则使用(设置为setContentView或layoutInFlayout)第二个布局,否则使用默认值。

你明白了吗?

if (userSelected == true){
   setContentView(R.layout.a)
} else{
setContentView(R.layout.b)
}
相关问题