java - 如何更改 PreferenceActivity 主题?

标签 java android

我试图在我的应用程序中更改 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);

}

结果是:

Result

最佳答案

您是否尝试过在 list 中的 Activity 标签上应用主题?这是我以前的做法 -

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

关于java - 如何更改 PreferenceActivity 主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11751498/

相关文章:

java - Android Studio 无法找到显式 Activity 类

Java ResultSet 如何在 UTC 中获取时间戳

c# - 如何将流中的图像另存为在 retrofit2 中发送到 wcf web 服务

java - 如何通过 Web 服务传递自定义对象

Android:如何将背景图像设置为给定 "wrap_content"的按钮

java - 锁定 ConcurrentHashMap 的值对象

java - 如何使用 Maven 创建具有依赖项的可执行/可运行 JAR?

Android - 多个电子邮件地址作为输入

javascript - 通过 JavaScript 在 Android 浏览器中禁用触摸手势

java - <spring :bind> for? 什么时候用,什么时候不用?