首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓EditTextPreference风格

安卓EditTextPreference风格
EN

Stack Overflow用户
提问于 2014-01-23 22:45:12
回答 4查看 5.6K关注 0票数 6

在我的应用程序设置中,我使用的是EditTextPreference,在android API中,11编辑文本字段有黑色的背景和黑色的文本颜色。如何挑战EditTextPreferences背景色?

我尝试过:在主题中:

代码语言:javascript
复制
<item name="android:editTextPreferenceStyle">@style/EditTextAppTheme</item>

在风格上:

代码语言:javascript
复制
<style name="EditTextAppTheme" parent="android:Widget.EditText">
      <item name="android:background">@drawable/edit_text_holo_light</item>
      <item name="android:textColor">#000000</item>
  </style>

如果我将样式设置为:

代码语言:javascript
复制
<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>

主题为:

代码语言:javascript
复制
<item name="android:editTextPreferenceStyle">@style/EditTextPreferences</item>

我收到错误:

代码语言:javascript
复制
error: Error retrieving parent for item: No resource found that matches the given name 'android:Preference.DeviceDefault.DialogPreference.EditTextPreference'.
EN

回答 4

Stack Overflow用户

发布于 2014-01-26 02:35:35

在过去的几天里,我也一直在试图弄清楚这一点。我发现所有的EditTextPrefence扩展都是AlertDialogEditText。因此,如果你能在你的主题中设计这两种样式,你就会在EditTextPreference中找到你想要的结果。以下是我正在使用的内容:

代码语言:javascript
复制
<?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>属性。我找到了可样式化的资源here,更具体地说是alertDiaolg here (注意:您也可以在您的计算机上的SDK目录中找到它们)

我希望这至少能把你引向正确的方向,我一直在努力弄清楚这一点(我在应用程序中的第一个主题)。祝你编码愉快!

票数 7
EN

Stack Overflow用户

发布于 2014-12-20 05:22:44

我认为您所观察到的效果是姜饼模拟器中的一个bug。在更改活动的主题并重新启动模拟器之后,EditTextPreference显示如下。

票数 0
EN

Stack Overflow用户

发布于 2015-02-03 06:25:50

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

代码语言:javascript
复制
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中全部替换

代码语言:javascript
复制
<EditTextPreference ... />

使用

代码语言:javascript
复制
<your.package.CustomEditTextPreference ... />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21311384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档