13

我正在尝试在我的应用程序中更改 PreferenceActivity 的主题,但我无法让它工作。

这是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>

这是 PreferenceActivity:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    this.setTheme(R.style.AppTheme);

    addPreferencesFromResource(R.xml.preferences);

}

结果是:

结果

4

5 回答 5

18

您是否尝试在清单中的活动标签上应用主题?这就是我之前的做法-

<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>

编辑:

您可以尝试的另一个选项是覆盖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);
}
于 2012-08-01T01:48:24.363 回答
10

最后我发现了如何以编程方式更改“PreferenceActivity”的主题(通过java代码)

要更改主题,只需这样做:

        @Override
        public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Holo_Theme_Light);
        super.onCreate(savedInstanceState);
        }

总是setTheme(R.style.yourtheme);在方法之前调用super.onCreate(savedInstanceState);方法。通过这样做,它将产生如下所示的结果。

在此处输入图像描述

就这样。

setTheme(R.style.yourtheme);如果你在方法之后调用方法super.onCreate(savedInstanceState);,它将产生如下所示的结果。

在此处输入图像描述

注意:嵌套的 PreferenceScreen 无法识别主题。要将主题应用于该嵌套的 PreferenceScreen,您必须为该嵌套的 PreferenceScreen 创建另一个 PreferenceActivity 并setTheme(R.style.yourtheme);在那里调用方法。

于 2013-03-27T10:11:52.010 回答
4

如果你想改变背景,你可以使用

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); 

           .......
    }

}

于 2012-08-01T09:44:05.790 回答
0

我个人使用这种方法:对于 11 以下的 API,我使用(值目录,themes.xml 文件):

  <?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="@android:style/Theme.Light.NoTitleBar"></style>

</resources>

对于更高版本(例如 values-v14 目录、themes.xml 文件):

    <?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>

</resources>

并表现:

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

Android 会根据设备的 API 自动选择主题,无需在活动(清单)或代码中的语法上指定任何主题...

主题

于 2013-12-12T10:28:04.777 回答
0

这听起来很愚蠢,但 usingsetTheme()将适用于您的所有片段,但不是主要的偏好活动。在清单文件中设置它,它会突然开始工作。

我根据用户偏好动态设置主题,setTheme()在 from 主题管理器对象之前调用onCreate(它还提供各种颜色,drawables并且可以通过我不想在样式中设置的主题进行更改......主要是因为它太混乱了弄清楚什么是什么)。

不知道为什么在清单中设置它会setTheme()起作用。我猜只是一些特定于偏好活动的故障。

于 2015-05-18T06:12:46.510 回答