简体   繁体   English

Android:DialogPreference颜色/样式?

[英]Android: DialogPreference color/style?

My main preference activity is set to "@android:style/Theme.Light" . 我的主要偏好活动设置为"@android:style/Theme.Light" One of my preferences is a DialogPreference who's Dialog contains a ListView. 我的一个偏好是DialogPreference,其Dialog包含ListView。 The ListView's dialog is dark grey (because DialogPreference uses AlertBuilder which creates dark grey dialogs) and the text in the list is black (because Theme.Light causes listViews to have black text). ListView的对话框为深灰色(因为DialogPreference使用AlertBuilder创建深灰色对话框),列表中的文本为黑色(因为Theme.Light导致listViews有黑色文本)。 Is there an easy way to either get the ListView to behave with the same style as the dark dialog? 是否有一种简单的方法可以使ListView的行为与黑暗对话框相同? Or to get the dark dialog to behave with the same style as the light activity? 或者让黑暗的对话框表现出与灯光活动相同的风格?

EDIT: 编辑:

Based on Merlin's comments, it seems like what I should try to do is create a LightDialog Theme. 根据Merlin的评论,似乎我应该尝试做的是创建一个LightDialog主题。 In order to do this I tried: 1. extending android's Theme.Light and adding the dialogy properties from Theme.Dialog 为了做到这一点,我试过: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>

... and 2. extending android's Theme.Dialog and adding the lighty properties from Theme.Light. ...和2.扩展android的Theme.Dialog并添加Theme.Light的轻微属性。

   <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> 

Both of these attempts failed because they use non-public attributes. 这两种尝试均失败,因为它们使用非公共属性。 Any suggestions on how to create a LightDialog theme? 有关如何创建LightDialog主题的任何建议?

You can inherit defult themes when creating styles in Android. 在Android中创建样式时,您可以继承defult主题。

Please read the documentation on Applying Styles and Themes specifically the section on inheritance 请阅读有关应用样式和主题的文档,特别是有关继承的部分

You should be able to take Theme.light and correct any issues that you have with it. 您应该能够使用Theme.light并更正您遇到的任何问题。

You may find that different vendors alter the themes on their devices so if you are targeting a broad range of hardware then you may be better creating a full theme to ensure that your app is consistent on all platforms. 您可能会发现不同的供应商会更改其设备上的主题,因此如果您的目标是广泛的硬件,那么您可能最好创建一个完整的主题,以确保您的应用在所有平台上都是一致的。

UPDATE UPDATE

As stated in this answer Consistent UI color in all Android devices there are public and non-public attributes. 如此答案中所述, 所有Android设备中的一致UI颜色都有公共和非公共属性。 The answer provides a link to a list of public attributes however kernel.org is still down so you will need to dig through the source for core/res/res/values/public.xml 答案提供了一个公共属性列表的链接,但是kernel.org仍处于关闭状态,因此您需要挖掘核心/ res / res / values / public.xml的源代码

You can specify style of DialogPreference 's dialog using android:alertDialogTheme (supported starting from API 11 ) them property of preferences activity: 您可以使用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>

I have a solution how you can do it programmatically (I think its the easier way): 我有一个解决方案,你怎么能以编程方式(我认为它更简单的方式):

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

...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM