我在以编程方式设置“主题切换器”时遇到了一些困难。 我想从应用程序切换主题(在白色(Theme.Light.NoTitleBar)和黑暗(Theme.Black.NoTitleBar)之间),我所做的是: 我设置了一个 SharedPreference:
final String PREFS_NAME = "MyPrefsFile";
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
final SharedPreferences.Editor editor = settings.edit();
而且我有两个按钮来切换主题(第二个几乎相同)
Button ThemeWhite = (Button) findViewById(R.id.ThemeWhite);
ThemeWhite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
editor.putBoolean("Theme", false);
editor.commit();
System.exit(2);
}
});
在请求每个 Activity 时,我都会检查 SharedPreference
boolean theme = settings.getBoolean("Theme", false);
if(theme){
this.setTheme(R.style.Theme_NoBarBlack);
} else{
this.setTheme(R.style.Theme_NoBar);
}
setContentView(R.layout.aplikacja);
我在文件夹值的 styles.xml 文件中定义主题:
<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.Light.NoTitleBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.NoTitleBar" />
在 values-v11 中:
<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.Holo.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.Holo.NoActionBar" />
在 values-v14 中:
<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.DeviceDefault.NoActionBar" />
list 文件:
<application
...
android:theme="@style/Theme.NoBar" >
在 android >4.0 上一切都运行良好 但是当我使用 2.2 时它不会改变主题 - 只是字体变白了,但没有深色背景。
我尝试检查它是否至少有效并更改了 Theme.NoBarBlack 的值(对于 android <3.0)及其值与 Theme.NoBar 相同,然后当我按下按钮时字体没有改变 - 正如它应该做的那样。
编辑(发现问题): 因此,在尝试后发现在 Android 2.2 Manifest 文件中设置了背景和休息,并且以编程方式更改主题仅影响文本颜色。知道为什么会这样吗?
回答(因为我没有 10 个声望): 在 android <3.0 上,如果
很重要super.onCreate(savedInstanceState);
是否设置主题前。
所以应该是:
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_NoBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
}
抱歉大家无中生有地制造问题,但因为它正在 >3.0 上工作,我很困惑。花半天时间在上面但工作。 :)
最佳答案
您的 styles.xml 中有一个错误:
对于 Theme.NoBarBlack
,您将父级设置为 @android:style/Theme.NoActionBar
,但它不存在。
我想你的意思是@android:style:Theme.NoTitleBar
。
关于android - 主题更改不适用于 <4.0,因为它应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12764087/