Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用户可以更改应用程序主题_Java_Android_Eclipse_Themes_Android Actionbar - Fatal编程技术网

Java 用户可以更改应用程序主题

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_

我希望我的应用程序的用户能够更改应用程序主题。 例如,深色、浅色、浅色,带有深色操作栏和设备默认值。这是怎么可能的,我有一个prefrences屏幕,其中有一个listpref,有四个选项(如上所示)。如何让用户更改应用程序主题


谢谢

这里是一个如何做的例子

 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 Instebo
private int mCurrentTheme在您的活动中。