Mic*_*ert 5 android themes styles android-layout android-theme
以编程方式设置"主题切换器"时遇到一些困难.我想从app(白色(Theme.Light.NoTitleBar)和Dark(Theme.Black.NoTitleBar)之间)切换主题,我的工作是:我设置了一个SharedPreference:
final String PREFS_NAME = "MyPrefsFile";
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
final SharedPreferences.Editor editor = settings.edit();
Run Code Online (Sandbox Code Playgroud)
而且我有两个按钮来切换主题(第二个几乎相同)
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);
}
});
Run Code Online (Sandbox Code Playgroud)
在每个活动的乞讨中,我检查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);
Run Code Online (Sandbox Code Playgroud)
我在文件styles.xml中定义文件夹值中的主题:
<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.Light.NoTitleBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.NoTitleBar" />
Run Code Online (Sandbox Code Playgroud)
在值-v11中:
<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.Holo.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.Holo.NoActionBar" />
Run Code Online (Sandbox Code Playgroud)
在值-v14中:
<resources>
<style name="Theme.NoBar" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="@android:style/Theme.DeviceDefault.NoActionBar" />
Run Code Online (Sandbox Code Playgroud)
清单文件:
<application
...
android:theme="@style/Theme.NoBar" >
Run Code Online (Sandbox Code Playgroud)
一切都在android> 4.0上工作得非常好但是当我使用2.2时它不会改变主题 - 只是字体变得白皙应该是,但没有深色背景.
我试过检查它是否至少有效并且更改了值(对于android <3.0)的Theme.NoBarBlack,其值与Theme.NoBar相同,然后当我按下按钮字体时没有改变 - 它应该这样做.
编辑(发现问题):所以在尝试之后,在Android 2.2 Manifest文件集背景和休息,并以编程方式更改主题仅影响文本颜色.知道为什么会这样吗?
答案(因为我没有10个声誉): 在android <3.0上它很重要
super.onCreate(savedInstanceState);
Run Code Online (Sandbox Code Playgroud)
是否在设置主题之前.
所以它应该是:
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_NoBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
}
Run Code Online (Sandbox Code Playgroud)
对不起,因为它工作在3.0以上的问题,我很困惑.花一半时间,但工作.:)
您的 styles.xml 中存在错误:对于Theme.NoBarBlack,您将父项设置为@android:style/Theme.NoActionBar,但它不存在。
我想你的意思是@android:style:Theme.NoTitleBar。
| 归档时间: |
|
| 查看次数: |
2343 次 |
| 最近记录: |