6

在我的设置应用程序中,我使用 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'.
4

4 回答 4

7

在过去的几天里,我也一直试图弄清楚这一点。我发现所有的EditTextPrefence扩展都是AlertDialogand EditText。因此,如果您可以在主题中设置这两个样式,您会在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 资源,更具体地说是 alertDiaolg here(注意:这些也可以在您计算机上的 SDK 目录中找到)

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

于 2014-01-25T18:35:34.547 回答
0

首先 extendEditTextPreference和 override 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然后全部替换

<EditTextPreference ... />

<your.package.CustomEditTextPreference ... />
于 2015-02-02T22:25:49.677 回答
0

http://i.imgur.com/zr0VUzY.jpg

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

于 2014-12-19T21:22:44.383 回答
0

当我切换到 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。

于 2015-02-22T10:35:26.467 回答