如何改变preferenceActivity主题?主题、preferenceActivity

2023-09-05 01:39:05 作者:本小姐只对Ní有兴趣々

我试图改变主题为preferenceActivity在我的应用程序,我只是无法得到它的工作。

I'm trying to change the theme for the PreferenceActivity in my app and I just can't get it to work.

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

And this is the PreferenceActivity:

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

    this.setTheme(R.style.AppTheme);

    addPreferencesFromResource(R.xml.preferences);

}

和结果是:

推荐答案

您是否尝试过将主题在清单中的活动标签?这是我怎么做之前 -

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>

编辑:

另一种选择,你可以尝试是覆盖 onApplyThemeResource(Resources.Theme主题,诠释渣油,布尔第一)。看着C中的setTheme将在内部调用方法Android源$ C ​​$。

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.

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