EditText背景问题(安卓)

8

我有一个关于EditText背景的问题,就像这样:

<EditText
     android:id="@+id/edit"
     android:layout_width="fill_parent"
     android:layout_height="35sp"                                                                       
     android:singleLine="true"

     android:layout_marginLeft="5px"
     android:layout_marginRight="5px"
     android:layout_marginTop="5px"
     android:textAppearance="?android:attr/textAppearanceSmall"      
      />

图片 http://i765.photobucket.com/albums/xx299/trieutrinhtrinh/edittext.jpg

尝试设置背景后,它看起来更糟了。

<EditText
     android:id="@+id/edit"
     android:layout_width="fill_parent"
     android:layout_height="35sp"                                                                       
     android:singleLine="true"

     android:layout_marginLeft="5px"
     android:layout_marginRight="5px"
     android:layout_marginTop="5px"
     android:textAppearance="?android:attr/textAppearanceSmall"
     android:background="#ffffff"    
      />

alt text http://i765.photobucket.com/albums/xx299/trieutrinhtrinh/edittext2.jpg

EditText的背景发生了什么问题?如何使EditText保持默认样式?


这不是android:textAppearance="@android:attr/textAppearanceSmall"吗? - Bostone
实际上 - 文本的语法是正确的,请参见我下面的答案。 - Bostone
8个回答

16

以下是我之前调查得出的2种更改EditText背景的解决方案,希望能对你有所帮助:

问题: 当将背景设置为EditText时,它看起来很糟糕。

分析: EditText使用九宫格图像作为背景。它们使用选择器根据当前EditText状态(启用/焦点/按下,默认)更改背景图像。

有两种解决此问题的方法,每种方法都有优缺点:

解决方案1: 创建自定义EditText(遵循此解决方案,我们可以自由地更改EditText视图)。

优点: 您可以自由渲染EditText视图以符合您的目的,无需像Android EditText的当前实现那样创建九宫格图像。(您必须在EditText中提供IF,以便根据EditText的状态(启用/聚焦...)平滑更改背景。) 可重复使用,在您想要更改背景颜色或添加更多颜色的情况下更加自定义。

缺点: 创建和测试自定义EditText需要花费很多精力。 (我选择了解决方案2,所以没有实现解决方案1的演示,如果有人遵循此解决方案,请随时与我们分享您的演示代码)

解决方案2: 使用Android实现中的选择器

❶创建名为edittext_selector.xml的xml文件

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edittext_selector">
    <!-- Image display in background in select state -->    
    <item 
        android:state_pressed="true" 
        android:drawable="@drawable/your_ninepath_image">
    </item>

        <!-- Image display in background in select state -->    
    <item 
        android:state_enabled="true" 
        android:state_focused="true" 
        android:drawable="@drawable/your_ninepath_image">
    </item>

    <!-- Default state --> 
    <item android:state_enabled="true" 
        android:drawable="@drawable/your_ninepath_image">
    </item> 
</selector>

❷在EditText的xml中设置selector:

<EditText 
    ...
    android:background="@layout/**edittext_selector**"
    ...
</EditText> 

Note:

● 在此演示代码中,我已经删除了一些视图状态的行为,请参考Android实现以获取详细行为(聚焦/取消聚焦,启用/禁用,按下,选定等)。

● 注意选择器中项目的顺序。在选择器XML文件中对项目的不同顺序将具有不同的背景色。

优点: 简单易懂,只需创建选择器并将其设置为后台,在需要更多颜色时,只需设置更多选择器,然后通过代码设置即可。

缺点: 需要创建九宫格图像作为选择器,如果您想要更改颜色或添加更多颜色,则必须创建更多图像和选择器。因此,它比Solution1要不够健壮。

这是我研究处理图像背景的方法,可能正确也可能错误,如果您有更好的解决方案或解释,请随时与我们分享。

我已经实现了解决方案2,并且它有效。


12

我的解决方案只需要一行代码:

<your-widget-component-that-has-a-background-color>.getBackground().setColorFilter(Color.<your-desired-color>, PorterDuff.Mode.MULTIPLY);).
这是这样分解的:
- "getBackground()" 从组件中提取背景 - "setColorFilter" 将在背景图像本身上调用过滤器 - "Color." 确定你想传递给过滤器的颜色 - "PorterDuff.Mode." 设置您希望对给定颜色和提取的背景图像执行的操作类型。
了解图像编辑软件的人可能会认识这些模式。每种模式对如何将颜色应用于背景图像有一定的影响。要简单地“覆盖”图像的颜色,同时保留其渐变、边框等,请使用“MULTIPLY”。

4

如果您希望在不完全更改背景图像的情况下即时编辑Android背景颜色,请尝试以下方法(这可能不是最佳解决方案,但它有效):

YourEditText.getBackground().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.MULTIPLY);

4
如果您将EditText背景设置为一种颜色,您将有效地压制Android的默认背景,该背景可能是一个Nine Patch,但绝不仅仅是一个简单的颜色。结果- 您将获得EditText的最简形式- 一个正方形框。这里是稍微过时的 内置可绘制列表,以便给您一些想法。

3
您不需要创建图像。在Android系统中有一个内置的图像可供使用。因此,请按照以下方法在XML中编辑您的EditText:
<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="10dp"
    android:hint="@string/campaign_message"
    android:inputType="textMultiLine"
    android:background="@android:drawable/editbox_background_normal"
    android:minHeight="80dp" >
</EditText>

请注意这一行:android:background="@android:drawable/editbox_background_normal" 它是关于Android技术的。

你是否理解错了?当你设置编辑文本的背景颜色时,内置的可绘制对象会被覆盖。除非你已经将EditText主题从默认的可绘制对象中移除,否则甚至没有使用“android:background = @ android:drawable / editbox_background_normal”的理由。 - Dandre Allison

2
我认为你应该更改背景颜色,而不是更改背景。因为它使用了xml自定义形状。
  • 一个要用作背景的可绘制对象。这可以是对完整可绘制资源的引用(例如PNG图像、9-patch、XML状态列表描述等),也可以是纯色,如#ff000000(黑色)。

  • 可能是对另一个资源的引用,形式为@[+][package:]type:name或对主题属性的引用,形式为?[package:][type:]name

  • 可以是颜色值,形式为#rgb#argb#rrggbb#aarrggbb


他使用的背景颜色语法是有效的。 - Bostone
好的,关于语法,我认为他用阴影等覆盖默认的背景 XML 形状,并将其设置为纯色。 - Shtirlic

1

0

我必须使用 SRC_ATOP 才能让它对我起作用

mEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);

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