安卓EditText与背景融合问题

9
我的应用程序使用 Theme.Holo.Light.DarkActionBar 作为父主题。
当我在Android 3.2平板电脑模拟器上使用时,EditText的形状几乎无法看到。它看起来像是试图在白色背景上绘制白色。如下所示: enter image description here 当我在Android 4.0平板电脑模拟器上使用时,EditText的形状看起来很好。您可以看到EditText底部的深灰线条。如果您查看上面的图片,您将仅在与浅灰色背景水印相交处的同一位置看到白色线条。 enter image description here 以下是我的布局中的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显示的颜色不正确?我没有覆盖绘制代码或背景图。

嗨Kenny Wyland,你解决问题了吗?我也遇到了同样的问题。 - Asish AP
我从未找出问题所在,但我的解决方法是从Ice Cream Sandwich中复制.9.png和样式,并将其硬编码到我的应用程序中,以供Honeycomb和Ice Cream Sandwich使用。 - Kenny Wyland
请查看以下链接:https://dev59.com/xGox5IYBdhLWcg3wazmQ?answertab=active#tab-top - DKV
https://dev59.com/xGox5IYBdhLWcg3wazmQ?answertab=active#tab-top - DKV
3个回答

5
其他答案并不是解决我的问题的真正方法,我也从未弄清楚到底是什么导致了这个问题。然而,我是这样解决的:我的解决办法是从Ice Cream Sandwich中复制.9.pngs和EditText小部件的样式,并在我的应用程序中硬编码为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中列出了文件名。


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

1

如果您使用以下主题,您可以拥有一个操作栏并仍然看到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”。


0

我解决了我的问题。在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">

试试这个。


2
我正在使用标准的Android Theme.Holo主题,但在3.0之前它并不存在,因此我使用Google提供的标准代码根据操作系统版本在Theme和Theme.Holo之间进行切换。问题不在于我没有使用主题...问题在于Honeycomb上的Theme.Holo在某些设备上表现不正常。 - Kenny Wyland
我有同样的问题。我正在Motorola Xoom平板电脑上测试我的应用程序。我已经设置了Theme.Holo,但是没有任何变化(EditText仍然与背景混合)。可能我还需要进行一些硬编码。 - Sandra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接