Java 用户可以更改应用程序主题
我希望我的应用程序的用户能够更改应用程序主题。 例如,深色、浅色、浅色,带有深色操作栏和设备默认值。这是怎么可能的,我有一个prefrences屏幕,其中有一个listpref,有四个选项(如上所示)。如何让用户更改应用程序主题Java 用户可以更改应用程序主题,java,android,eclipse,themes,android-actionbar,Java,Android,Eclipse,Themes,Android Actionbar,我希望我的应用程序的用户能够更改应用程序主题。 例如,深色、浅色、浅色,带有深色操作栏和设备默认值。这是怎么可能的,我有一个prefrences屏幕,其中有一个listpref,有四个选项(如上所示)。如何让用户更改应用程序主题 谢谢这里是一个如何做的例子 if (prefs.getBoolean("1darkTheme", false)==false){//user has selected dark theme setTheme(android.R.style.Theme_
谢谢这里是一个如何做的例子
if (prefs.getBoolean("1darkTheme", false)==false){//user has selected dark theme
setTheme(android.R.style.Theme_Holo);
Toast.makeText(getApplicationContext(), "dark", Toast.LENGTH_SHORT).show();
} else {
setTheme(android.R.style.Theme_Holo_Light);
Toast.makeText(getApplicationContext(), "light", Toast.LENGTH_SHORT).show();
}
setContentView(R.layout.main);
下面是一个如何做到这一点的例子
if (prefs.getBoolean("1darkTheme", false)==false){//user has selected dark theme
setTheme(android.R.style.Theme_Holo);
Toast.makeText(getApplicationContext(), "dark", Toast.LENGTH_SHORT).show();
} else {
setTheme(android.R.style.Theme_Holo_Light);
Toast.makeText(getApplicationContext(), "light", Toast.LENGTH_SHORT).show();
}
setContentView(R.layout.main);
如果您只想使用内置主题,而不需要自定义它们,那么这是非常容易的 作为一个例子,我将使用
ListPreferece
和如下输入值:
<string-array name="pref_theme_values" translatable="false">
<item>THEME_LIGHT</item>
<item>THEME_DARK</item>
</string-array>
之后,您应该重写onCreate
方法:
// onCreate
this.mCurrentTheme = this.getThemeId(this);
this.setTheme(this.mCurrentTheme);
this.setContentView(...); // should be after the setTheme call
和onStart
方法(因为用户从settigns页面返回后,您需要立即刷新主题)
此外,您还需要以某种方式保存“活动”状态,以便应用程序在“活动”重新启动后显示相同的数据。如果您只想使用内置主题,而不需要自定义它们,则非常容易 作为一个例子,我将使用
ListPreferece
和如下输入值:
<string-array name="pref_theme_values" translatable="false">
<item>THEME_LIGHT</item>
<item>THEME_DARK</item>
</string-array>
之后,您应该重写onCreate
方法:
// onCreate
this.mCurrentTheme = this.getThemeId(this);
this.setTheme(this.mCurrentTheme);
this.setContentView(...); // should be after the setTheme call
和onStart
方法(因为用户从settigns页面返回后,您需要立即刷新主题)
您还需要以某种方式保存活动状态,以便应用程序在活动重新启动后显示相同的数据。是的,您可以这样做。我在以前的项目中就是这么做的。但是我手头没有工作用的笔记本电脑,所以我无法向您展示一些示例代码。这就是为什么我只添加注释而不是答案的原因。是的,你可以这样做。我在以前的项目中就是这么做的。但是我手头没有工作用的笔记本电脑,所以我无法向您展示一些示例代码。这就是为什么我只添加注释而不是答案的原因。我如何定义mCurrentTheme?结构,我没有全部明白,但是谢谢@Stian Instebo
private int mCurrentTheme代码>在活动中。我如何定义mCurrentTheme?结构,我没有全部明白,但是谢谢@Stian Insteboprivate int mCurrentTheme代码>在您的活动中。