【问题标题】:Android EditTextPreference styleAndroid EditTextPreference 样式
【发布时间】:2014-02-14 04:34:26
【问题描述】:

在我的应用程序设置中,我使用 EditTextPreference ,而在 android API uder 11 上,edittext 字段具有黑色背景和黑色文本颜色。 如何更改 EditTextPreferences 背景颜色?

我试过了: 在主题中:

<item name="android:editTextPreferenceStyle">@style/EditTextAppTheme</item>

风格:

<style name="EditTextAppTheme" parent="android:Widget.EditText">
      <item name="android:background">@drawable/edit_text_holo_light</item>
      <item name="android:textColor">#000000</item>
  </style>

如果我将样式设置为:

<style name="EditTextPreferences" parent="android:Preference.DeviceDefault.DialogPreference.EditTextPreference">
      <item name="android:background">@drawable/edit_text_holo_light</item>
      <item name="android:textColor">#FF0000</item>
  </style>

主题为:

<item name="android:editTextPreferenceStyle">@style/EditTextPreferences</item>

我收到错误:

error: Error retrieving parent for item: No resource found that matches the given name 'android:Preference.DeviceDefault.DialogPreference.EditTextPreference'.

【问题讨论】:

    标签: java android styles android-edittext edittextpreference


    【解决方案1】:

    在过去的几天里,我也一直在尝试解决这个问题。我发现所有的EditTextPrefence 扩展都是AlertDialogEditText。因此,如果您可以在您的主题中设置这两个样式,您将在 EditTextPreference 中找到您正在寻找的结果。这是我正在使用的:

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    
    <style name="LightTheme" parent="android:Theme.Light">
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@color/greyscale2</item>
        <item name="android:textColor">@color/greyscale16</item>
        <item name="android:textViewStyle">@style/LightTextView</item>
        <item name="android:buttonStyle">@style/LightButton</item>
        <item name="android:editTextStyle">@style/LightEditText</item>
        <item name="android:alertDialogTheme">@style/LightDialog</item>
        <item name="android:dialogPreferenceStyle">@style/LightDialog</item>
        <item name="android:colorBackground">@color/greyscale2</item>
        <item name="android:colorBackgroundCacheHint">@color/greyscale1</item>
    </style>
    
    <!-- TextView -->
    <style name="LightTextView" parent="android:Widget.TextView">
        <item name="android:textColor">@color/greyscale16</item>
        <item name="android:background">@android:color/transparent</item>
    </style>
    
    <!-- Button -->
    <style name="LightButton" parent="android:Widget.Button">
        <item name="android:textColor">@color/greyscale16</item>
        <item name="android:background">@drawable/light_button_background</item>
        <item name="android:gravity">center_vertical|center_horizontal</item>
    </style>
    
    <style name="LightCheckBox" parent="android:Widget.CompoundButton.CheckBox">
        <item name="android:background">@color/greyscale2</item>
    </style>
    
    <style name="LightEditText" parent="android:style/Widget.EditText">
        <item name="android:background">@color/greyscale1</item>
        <item name="android:editTextBackground">@color/greyscale2</item>
        <item name="android:textColor">@color/greyscale16</item>
        <item name="android:button">@style/DarkButton</item>
        <item name="android:inputType">number</item>
    </style>
    
    <style name="LightDialog" parent="@android:style/Theme.Dialog">
        <item name="android:background">@color/greyscale2</item>
        <item name="android:textColor">@color/greyscale16</item>
        <item name="android:textViewStyle">@style/LightTextView</item>
        <item name="android:buttonStyle">@style/LightButton</item>
        <item name="android:divider">@color/greyscale14</item>
    </style>
    

    注意我的基本主题中的 <item name="android:dialogPreferenceStyle">@style/LightDialog</item> 属性。我找到了 styleable 资源 here,更具体地说是 alertDiaolg here(注意:这些也可以在您计算机上的 SDK 目录中找到)

    我希望这至少可以引导您朝着正确的方向前进,我一直在努力解决这个问题(我在应用程序中的第一个主题)。快乐编码!

    【讨论】:

    • android:alertDialogTheme 为我做了!我只是想设计当你按下时出现的小子对话框,例如EditTextPreference。谢谢!
    【解决方案2】:

    我认为您观察到的效果是 Gingerbread 模拟器中的一个错误。 更改活动主题并重新启动模拟器后,EditTextPreference 出现如上。

    【讨论】:

      【解决方案3】:

      首先扩展 EditTextPreference 并覆盖 onPrepareDialogBuilder,反转 Honeycomb 以下版本的背景颜色:

      public class CustomEditTextPreference extends EditTextPreference {
      
          /**
           * Prepares the dialog builder to be shown when the preference is clicked.
           * Use this to set custom properties on the dialog.
           * <p>
           * Do not {@link AlertDialog.Builder#create()} or
           * {@link AlertDialog.Builder#show()}.
           */
          @Override
          protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
              builder.setInverseBackgroundForced(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB);
          }
      }
      

      然后替换你的preferences.xml all

      <EditTextPreference ... />
      

      <your.package.CustomEditTextPreference ... />
      

      【讨论】:

        【解决方案4】:

        当我切换到 API v21 AppCompat 时,我遇到了这个确切的问题(我认为 - 它以前至少可以正常工作)。 我的解决方案是在我的主题中明确定义 android:editTextStyle 的样式 - 这是我完整的 styles.xml:

        <resources xmlns:android="http://schemas.android.com/apk/res/android">
        
            <!--
                Base application theme, dependent on API level. This theme is replaced
                by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
            -->
            <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        
                <!--
                    Theme customizations available in newer API levels can go in
                    res/values-vXX/styles.xml, while customizations related to
                    backward-compatibility can go here.
                -->
                <item name="android:editTextStyle">@android:style/Widget.EditText</item>
            </style>
        
            <!-- Application theme. -->
            <style name="AppTheme" parent="AppBaseTheme">
                <!-- All customizations that are NOT specific to a particular API-level can go here. -->
            </style>
        
        </resources>
        

        结果是一个与@Jesse Webb 的屏幕截图相同的editText。

        【讨论】:

          猜你喜欢
          • 2016-05-29
          • 2011-11-20
          • 1970-01-01
          • 1970-01-01
          • 2020-12-21
          • 2016-04-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多