Pau*_*aul 6 android android-preferences android-theme
我正在使用主题来自定义设置对话框的外观。首选项以XML定义,并以夸大PreferenceFragment。附加片段的方法基本上如开发人员指南中所述。
通过应用到托管活动的自定义主题,可以很好地自定义第一个屏幕:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Preferences_Dialog);
...
Run Code Online (Sandbox Code Playgroud)
搭配风格:
<style name="Theme.Preferences.Dialog" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:colorBackground">#fff0f0f0</item>
<item name="android:background">#fff0f0f0</item>
<item name="android:divider">#ffe0e0e0</item>
<item name="android:textColorPrimary">#ff555555</item>
<item name="android:textColorSecondary">#ff808080</item>
<item name="android:textAppearanceLarge">@style/preferences_large_text</item>
<item name="android:textAppearanceMedium">@style/preferences_medium_text</item>
</style>
Run Code Online (Sandbox Code Playgroud)
还有一些首选项定义如下:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/pref_title" >
...
<ListPreference
android:enabled="false"
android:key="@string/pref_change_workspace_key"
android:persistent="true"
android:summary="@string/pref_change_workspace_summary_singel"
android:title="@string/pref_change_workspace_title" />
...
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
麻烦的是,打开对话框的所有首选项(如ListPreference)都具有与其余对话框不同的样式。
设置片段的第一级看起来不错:

但是单击其中一个元素会产生错误的结果:

值 => 样式.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:dialogTheme">@style/MyDialogStyle</item>
<item name="android:alertDialogTheme">@style/MyDialogStyle</item>
</style>
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
values-v21 => 样式.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:dialogTheme">@style/MyDialogStyle</item>
<item name="android:alertDialogTheme">@style/MyDialogStyle</item>
</style>
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#fff0f0f0</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
上面看到的问题来自该行
<item name="android:background">#fff0f0f0</item>
Run Code Online (Sandbox Code Playgroud)
在定义的样式中。显然这个设置也用于生成的对话框。删除此行会产生预期结果。