java 如何更改 PreferenceActivity 主题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11751498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change PreferenceActivity theme?
提问by Eli Revah
I'm trying to change the theme for the PreferenceActivity in my app and I just can't get it to work.
我正在尝试更改我的应用程序中 PreferenceActivity 的主题,但我无法让它工作。
This is the xml:
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<SwitchPreference android:title="Auto Clear" android:key="autoclear" android:summary="Clear the command line when the code is being executed." android:defaultValue="false"/>
<ListPreference android:title="Choose a theme" android:negativeButtonText="" android:dialogTitle="" android:key="theme" android:entries="@array/themesList" android:entryValues="@array/themesList" android:defaultValue="Default" />
</PreferenceScreen>
And this is the PreferenceActivity:
这是 PreferenceActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setTheme(R.style.AppTheme);
addPreferencesFromResource(R.xml.preferences);
}
And the result is:
结果是:
回答by shri046
Have you tried applying the theme on the activity tag in the manifest? This is how I have done it before -
您是否尝试在清单中的活动标签上应用主题?我以前是这样做的——
<activity
android:label="@string/app_name"
android:name="com.example.MyPreferenceActivity"
android:theme="@android:style/Theme.Black"
android:exported="true"
android:icon="@drawable/ic_launcher"></activity>
EDIT:
编辑:
The other option you could try is to override onApplyThemeResource(Resources.Theme theme, int resid, boolean first)
. Looking at the android source code the setTheme will internally call method.
您可以尝试的另一个选项是覆盖onApplyThemeResource(Resources.Theme theme, int resid, boolean first)
. 查看 android 源代码 setTheme 将在内部调用方法。
/**
* Called by {@link #setTheme} and {@link #getTheme} to apply a theme
* resource to the current Theme object. Can override to change the
* default (simple) behavior. This method will not be called in multiple
* threads simultaneously.
*
* @param theme The Theme object being modified.
* @param resid The theme style resource being applied to <var>theme</var>.
* @param first Set to true if this is the first time a style is being
* applied to <var>theme</var>.
*/
protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
theme.applyStyle(resid, true);
}
回答by E Player Plus
At last i found out how to change theme of "PreferenceActivity" programmatically(via java code)
最后我找到了如何以编程方式更改“PreferenceActivity”的主题(通过 java 代码)
To change theme just do like this:
要更改主题,请执行以下操作:
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Holo_Theme_Light);
super.onCreate(savedInstanceState);
}
Always call setTheme(R.style.yourtheme);
method before super.onCreate(savedInstanceState);
method. By doing this it will produce result as shown below.
总是在setTheme(R.style.yourtheme);
方法之前调用super.onCreate(savedInstanceState);
方法。通过这样做,它将产生如下所示的结果。
That's all.
就这样。
If yo call setTheme(R.style.yourtheme);
method after super.onCreate(savedInstanceState);
method it will produce result as shown below.
如果setTheme(R.style.yourtheme);
在方法之后调用方法super.onCreate(savedInstanceState);
,它将产生如下所示的结果。
Note: Themes are not recognize by nested PreferenceScreen. To apply theme to that nested PreferenceScreen you have to make an another PreferenceActivity for that nested PreferenceScreen and call setTheme(R.style.yourtheme);
method there.
注意:嵌套的 PreferenceScreen 无法识别主题。要将主题应用于该嵌套的 PreferenceScreen,您必须为该嵌套的 PreferenceScreen 创建另一个 PreferenceActivity 并setTheme(R.style.yourtheme);
在那里调用方法。
回答by FrancescoAzzola
If you want to change the background you could use
如果你想改变背景,你可以使用
public class FractalPreferenceActivity extends PreferenceActivity {
.......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawableResource(R.drawable.gradient);
getListView().setBackgroundColor(Color.TRANSPARENT);
getListView().setCacheColorHint(Color.TRANSPARENT);
.......
}
}
}
回答by ODAXY
I personally use this approach: for API below 11 I use(values directory, themes.xml file):
我个人使用这种方法:对于低于 11 的 API,我使用(值目录,themes.xml 文件):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Light.NoTitleBar"></style>
</resources>
for higher (for example values-v14 directory, themes.xml file):
对于更高版本(例如 values-v14 目录、themes.xml 文件):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
</resources>
and manifest:
并体现:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Android will choose a theme based on device's API automatically, and no need to specify any themes in activities (manifest) or grammatically in code...
Android 会根据设备的 API 自动选择主题,无需在活动(清单)或代码中语法指定任何主题...
回答by Evan Langlois
This is gonna sound stupid, but using setTheme()
will work on all your fragments, but not the main preference activity. Set this in the manifest file, and it will suddenly start working.
这听起来很愚蠢,但 usingsetTheme()
将适用于您的所有片段,但不适用于主要的偏好活动。在清单文件中设置它,它会突然开始工作。
I set the theme dynamically based on user preferences, calling setTheme()
before the onCreate
from theme manager object (it also doles out various colors and drawables
that can be changed by the theme that I don't want to set in styles ... mainly because its too confusing to figure out what is what).
我根据用户偏好动态设置主题,setTheme()
在onCreate
from 主题管理器对象之前调用(它还发出各种颜色drawables
,可以通过我不想在样式中设置的主题来更改......主要是因为它太混乱了弄清楚什么是什么)。
No idea why setting it in the manifest makes setTheme()
work. Just some glitch specific to the preference activity I guess.
不知道为什么在清单中设置它会setTheme()
起作用。我猜只是一些特定于偏好活动的故障。