【问题标题】:Android: DialogPreference color/style?Android:DialogPreference 颜色/样式?
【发布时间】:2011-08-06 14:05:38
【问题描述】:

我的主要偏好活动设置为"@android:style/Theme.Light"。我的偏好之一是 DialogPreference,它的 Dialog 包含一个 ListView。 ListView 的对话框是深灰色的(因为 DialogPreference 使用创建深灰色对话框的 AlertBuilder)并且列表中的文本是黑色的(因为 Theme.Light 导致 listViews 具有黑色文本)。有没有一种简单的方法可以让 ListView 以与深色对话框相同的样式运行?还是让深色对话框与浅色 Activity 具有相同的样式?

编辑:

基于 Merlin 的 cmets,看来我应该尝试创建一个 LightDialog 主题。为了做到这一点,我尝试了: 1. 扩展 android 的 Theme.Light 并从 Theme.Dialog 添加对话属性

<style name="Theme.LightDialog" parent="@android:style/Theme.Light">
  <item name="android:windowFrame">@null</item> 
  <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item> 
  <item name="android:windowBackground">@android:drawable/panel_background</item> 
  <item name="android:windowIsFloating">true</item> 
  <item name="android:windowContentOverlay">@null</item> 
  <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
</style>

... 和 2. 扩展 android 的 Theme.Dialog 并添加来自 Theme.Light 的 lighty 属性。

   <style name="Theme.LightDialog" parent="@android:style/Theme.Dialog">
          <item name="android:windowBackground">@drawable/screen_background_light</item> 
          <item name="android:colorBackground">@android:color/background_light</item> 
          <item name="android:colorForeground">@androidcolor/bright_foreground_light</item> 
          <item name="android:colorForegroundInverse">@android:color/bright_foreground_light_inverse</item> 
          <item name="android:textColorPrimary">@android:color/primary_text_light</item> 
          <item name="android:textColorSecondary">@android:color/secondary_text_light</item> 
          <item name="android:textColorTertiary">@android:color/tertiary_text_light</item> 
          <item name="android:textColorPrimaryInverse">@android:color/primary_text_dark</item> 
          <item name="android:textColorSecondaryInverse">@android:color/secondary_text_dark</item> 
          <item name="android:textColorTertiaryInverse">@android:color/tertiary_text_dark</item> 
          <item name="android:textColorPrimaryDisableOnly">@android:color/primary_text_light_disable_only</item> 
          <item name="android:textColorPrimaryInverseDisableOnly">@android:color/primary_text_dark_disable_only</item> 
          <item name="android:textColorPrimaryNoDisable">@android:color/primary_text_light_nodisable</item> 
          <item name="android:textColorSecondaryNoDisable">@android:color/secondary_text_light_nodisable</item> 
          <item name="android:textColorPrimaryInverseNoDisable">@android:color/primary_text_dark_nodisable</item> 
          <item name="android:textColorSecondaryInverseNoDisable">@android:color/secondary_text_dark_nodisable</item> 
          <item name="android:textColorHint">@android:color/hint_foreground_light</item> 
          <item name="android:textColorHintInverse">@android:color/hint_foreground_dark</item> 
    </style> 

这两次尝试都失败了,因为它们使用了非公共属性。有关如何创建 LightDialog 主题的任何建议?

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用首选项活动的android:alertDialogTheme(从API 11 开始支持)指定DialogPreference 对话框的样式:

    <style name="PreferencesActivityTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">        
        <item name="android:alertDialogTheme">@style/Theme.MyDialog</item>
    </style>
    
    <style name="Theme.MyDialog" parent="@style/Theme.AppCompat.Light.Dialog">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    【讨论】:

    • 如果你有一个 AlertDialog,这可以工作,但我有一个首选项对话框并且 @style/myDialogPreferenceTheme 不起作用。有什么想法吗?
    【解决方案2】:

    我有一个解决方案,您可以如何以编程方式完成(我认为这是更简单的方法):

    public class CustomDialogPreference extends DialogPreference {
    
    ...
    
    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);
    
     //changing color of divider
        int divierId = getDialog().getContext().getResources()
                .getIdentifier("android:id/titleDivider", null, null);
        View divider = getDialog().findViewById(divierId);
        divider.setBackgroundColor(getContext().getResources().getColor(R.color.light_orange));
    
     //changing color of title textview
        int titleId = getDialog().getContext().getResources()
                .getIdentifier("android:id/alertTitle", null, null);
        TextView title = (TextView) getDialog().findViewById(titleId);
        title.setTextColor(getContext().getResources().getColor(R.color.light_orange));
    }
    
    ...
    
    }
    

    【讨论】:

      【解决方案3】:

      在 Android 中创建样式时可以继承默认主题。

      请阅读Applying Styles and Themes 上的文档,特别是关于继承的部分

      您应该能够使用 Theme.light 并纠正您遇到的任何问题。

      您可能会发现不同的供应商会更改其设备上的主题,因此如果您的目标是广泛的硬件,那么您最好创建一个完整的主题以确保您的应用在所有平台上都保持一致。

      更新

      正如这个答案Consistent UI color in all Android devices 中所述,有公共和非公共属性。答案提供了一个指向公共属性列表的链接,但是 kernel.org 仍然关闭,因此您需要深入挖掘 core/res/res/values/public.xml 的源代码

      【讨论】:

      • 我尝试根据您的建议创建主题,但没有成功。请查看我的编辑。
      • 这是一个单独的问题,应该按原样发布,而不是更改原文的上下文。
      • 很公平,我会单独发布并将其标记为已回答。
      • 我已经用一些额外的信息更新了我的答案,不幸的是,这很令人失望......这个 kernel.org 问题真的很烦人
      • 我粘贴了它...点击最终链接
      猜你喜欢
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多