如何使用自定义主题设置DialogPreference的样式?

时间:2013-07-07 08:55:17

标签: android styles themes

在我的应用程序中,我使用的是Theme.Holo和Theme.Holo.Light,没有任何问题。当使用Holo主题并单击DialogPreference / ListPreference时,弹出的对话框也以Holo为主题。对于Holo.Light来说也是如此。但是,当我的自定义主题(来自Holo.Light)设置了PreferencesActivity样式时,所有对话框都以Holo.Light为主题。我想我在主题中缺少某些东西。谁能帮助我?非常感谢!

这是我的主题代码:

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

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="GreenTheme" parent="android:Theme.Holo.Light">

    <item name="android:editTextBackground">@drawable/edit_text_holo_light</item>

    <item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewGreenTheme</item>

    <item name="android:listChoiceIndicatorMultiple">@drawable/btn_check_holo_light</item>

    <item name="android:listChoiceIndicatorSingle">@drawable/btn_radio_holo_light</item>

    <item name="android:buttonStyle">@style/ButtonGreenTheme</item>

    <item name="android:imageButtonStyle">@style/ImageButtonGreenTheme</item>

    <item name="android:dropDownSpinnerStyle">@style/SpinnerGreenTheme</item>

    <item name="android:tabWidgetStyle">@style/TabWidgetGreenTheme</item>

    <item name="android:progressBarStyleHorizontal">@style/ProgressBarGreenTheme</item>

    <item name="android:seekBarStyle">@style/SeekBarGreenTheme</item>

    <item name="android:buttonStyleToggle">@style/ToggleGreenTheme</item>

    <item name="android:listChoiceBackgroundIndicator">@drawable/list_selector_holo_light</item>

    <item name="android:activatedBackgroundIndicator">@drawable/activated_background_holo_light</item>

    <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb_holo</item>

    <item name="android:actionBarStyle">@style/ActionBar.Solid.Greenactionbar</item>

    <item name="android:buttonBarButtonStyle">@style/ButtonBarButtonStyleGreenTheme</item>

    <item name="android:preferenceStyle">@style/TimePickerDialogFragmentGreen</item>
  </style>

      <style name="TimePickerDialogFragmentGreen" parent="@android:style/Theme.Holo.Light.Dialog">
         <item name="android:editTextBackground">@drawable/edit_text_holo_light</item>

    <item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewGreenTheme</item>

    <item name="android:listChoiceIndicatorMultiple">@drawable/btn_check_holo_light</item>

    <item name="android:listChoiceIndicatorSingle">@drawable/btn_radio_holo_light</item>

    <item name="android:buttonStyle">@style/ButtonGreenTheme</item>

    <item name="android:imageButtonStyle">@style/ImageButtonGreenTheme</item>

    <item name="android:dropDownSpinnerStyle">@style/SpinnerGreenTheme</item>

    <item name="android:tabWidgetStyle">@style/TabWidgetGreenTheme</item>

    <item name="android:progressBarStyleHorizontal">@style/ProgressBarGreenTheme</item>

    <item name="android:seekBarStyle">@style/SeekBarGreenTheme</item>

    <item name="android:buttonStyleToggle">@style/ToggleGreenTheme</item>

    <item name="android:listChoiceBackgroundIndicator">@drawable/list_selector_holo_light</item>

    <item name="android:activatedBackgroundIndicator">@drawable/activated_background_holo_light</item>

    <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb_holo</item>

    <item name="android:actionBarStyle">@style/ActionBar.Solid.Greenactionbar</item>

    <item name="android:buttonBarButtonStyle">@style/ButtonBarButtonStyleGreenTheme</item>
    </style>
</resources>

2 个答案:

答案 0 :(得分:3)

我找到了this rather unformatted but otherwise nice answer

要点是,DialogPreference s AlertDialog是在没有the theme parameter的情况下创建的,这意味着它们会应用哪个主题android:alertDialogTheme指向。

所以我将这样的主题扩展为主题对话框:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/yellow_500</item>
        <item name="colorPrimaryDark">@color/yellow_a700</item>
        <item name="colorAccent">@color/purple_400</item>
        <item name="android:alertDialogTheme">@style/DialogTheme</item>
    </style>

    <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorPrimary">@color/yellow_500</item>
        <item name="colorPrimaryDark">@color/yellow_a700</item>
        <item name="colorAccent">@color/purple_400</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
    </style>

</resources>

colorPrimary中的colorPrimaryDarkcolorAccentAppTheme也是我想要应用于对话框的颜色。

请注意,我需要android:windowMinWidthMinor来阻止对话框水平折叠。

答案 1 :(得分:0)

我有澄清问题,但由于费率低,不能问他们。那么:
1.你的第一个风格是否正常工作?
2.您确定,主题@android:style / Theme.Holo.Light.Dialog包含您在TimePickerDialogFragmentGreen中声明的所有项目吗?例如:

<item name="android:tabWidgetStyle"> @style/TabWidgetGreenTheme</item>


3.您是否尝试过 Luksprog

提出的想法

最后:我使用类似的东西为我的EditTexts创建自定义样式:

 <style name="EditTextStyle" parent="@style/Indents">
 <item name="android:textColor">@android:color/black</item>
 <item name="android:background">@android:color/white</item>
 <item name="android:textColorHint">@android:color/darker_gray</item>
 <item name="android:textSize">14sp</item>
</style>
<style name="Indents" parent="@style/Margins">
    <item name="android:paddingBottom">@dimen/activity_vertical_padding</item>
    <item name="android:paddingTop">@dimen/activity_vertical_padding</item>
</style>
<style name="Margins">
    <item name="android:layout_marginTop">@dimen/activity_vertical_margin </item>
    <item name="android:layout_marginBottom">@dimen/activity_vertical_margin </item>
    <item name="android:layout_marginLeft">@dimen/activity_horizontal_margin </item>
    <item name="android:layout_marginRight">@dimen/activity_horizontal_margin </item>
</style>