【问题标题】:android EditText blends into backgroundandroid EditText 融入背景
【发布时间】:2012-04-11 15:36:55
【问题描述】:

我的应用使用Theme.Holo.Light.DarkActionBar 作为父主题。

当我使用我的 Android 3.2 平板电脑模拟器时,几乎看不到 EditText 形状。看起来它正在尝试在白色上绘制白色。在这里看到:

当我在我的 Android 4.0 平板电脑模拟器上使用它时,EditText 形状看起来还不错。您可以看到沿 EditText 底部的深灰色线。如果您查看上图,您几乎不会在与浅灰色背景水印相交的同一位置看到一条白线。

这是我在布局中的 EditText:

<EditText
    android:id="@+id/fieldName"
    style="@style/PlayerDetails.Field"
    android:capitalize="words" />

风格如下:

<style name="PlayerDetails.Field">
    <item name="android:layout_weight">0.65</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:layout_marginLeft">10dp</item>
</style>

为什么我的 EditText 绘制的颜色错误?我没有覆盖绘图代码或背景可绘制对象。

【问题讨论】:

标签: android android-edittext android-3.0-honeycomb android-4.0-ice-cream-sandwich


【解决方案1】:

其他答案实际上并不能解决我的问题,我从未弄清楚真正导致问题的原因。然而,这就是我解决它的方法:我的解决方法是从 Ice Cream Sandwich 复制 EditText 小部件的 .9.pngs 和样式,然后硬编码到我的 Honeycomb 和 Ice Cream Sandwich 应用程序中。

编辑:

我创建了一个名为 res/drawable-nodpi/edit_text_holo_light.xml 的文件,其中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_multiline="true" android:state_window_focused="false" android:state_enabled="true"  android:drawable="@drawable/textfield_multiline_default_holo_light" />
    <item android:state_multiline="true" android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_multiline_disabled_holo_light" />
    <item android:state_multiline="true" android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_multiline_activated_holo_light" />
    <item android:state_multiline="true" android:state_enabled="true" android:state_activated="true" android:drawable="@drawable/textfield_multiline_focused_holo_light" />
    <item android:state_multiline="true" android:state_enabled="true" android:drawable="@drawable/textfield_multiline_default_holo_light" />
    <item android:state_multiline="true" android:state_focused="true" android:drawable="@drawable/textfield_multiline_disabled_focused_holo_light" />
    <item android:state_multiline="true" android:drawable="@drawable/textfield_multiline_disabled_holo_light" />

    <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/textfield_default_holo_light" />
    <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_disabled_holo_light" />
    <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_activated_holo_light" />
    <iten android:state_enabled="true" android:state_activated="true" android:drawable="@drawable/textfield_focused_holo_light" />
    <item android:state_enabled="true" android:drawable="@drawable/textfield_default_holo_light" />
    <item android:state_focused="true" android:drawable="@drawable/textfield_disabled_focused_holo_light" />
    <item android:drawable="@drawable/textfield_disabled_holo_light" />
</selector>

然后我在我的styles.xml中创建了一个样式来设置:

<item name="android:background">@drawable/edit_text_holo_light</item>

然后我从 android sdk 中复制了 .9.png 文件并将它们放入 res/drawable-* 中。文件名列在上面的 xml 中。

【讨论】:

  • 我遇到了同样的问题,请告诉我你是如何解决这个问题的。
  • 我进入 android sdk 文件夹找到用于绘制标准文本字段的 .9.png 文件。例如,其中之一称为 textfield_activated_holo_light.9.png。我将所有这些文件复制到我的 res/drawable-v11 目录中,以便它们可用于 v11 之后(包括 v11)的任何 Android 版本。然后我添加了一个 drawable-nodpi/edit_text_holo_light.xml 并在其中复制了原始 Android 样式的样式。 (这里太多了,我会更新我的答案)
【解决方案2】:

如果您使用以下主题,您可以拥有一个操作栏,但仍会看到 EditText 浅灰色背景水印:

AndroidManifest.xml

<activity
  android:name=".ClassName"
  android:label="ClassName"
  android:theme="@style/MyTheme" >

styles.xml

<resources>
  <style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
  </style>
  <style name="MyActionBar" parent="android:Widget.Holo.ActionBar">
    <item name="android:background">@color/black</item>
  </style>
</resources>

这适用于主题“android:Theme.Holo.Light”,但不适用于主题“android:Theme.Holo”。

【讨论】:

    【解决方案3】:

    我发现了我的问题。通过在 manifest.xml 文件中的应用程序元素 android:theme="@android:style/Theme" 中设置主题。

    <application 
    
    android:label="@string/app_name" 
    
    android:icon="@drawable/logo" 
    
    android:vmSafeMode="false" 
    
    android:theme="@android:style/Theme">
    

    试试这个。

    【讨论】:

    • 我使用的是标准的android Theme.Holo,但它在3.0之前不存在,所以我使用Google提供的标准代码根据操作系统版本在Theme和Theme.Holo之间切换。问题不在于我没有使用主题。问题在于 Honeycomb 上的 Theme.Holo 无法在所有设备上正常运行。
    • 我也有同样的问题。我正在摩托罗拉 Xoom 平板电脑上测试我的应用程序。我设置了 Theme.Holo,但没有任何改变(EditText 仍然融入背景)。可能我还得做一些硬编码。
    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 2011-08-08
    • 1970-01-01
    • 2014-11-08
    • 2015-07-05
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多