EditText 背景问题 (android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1625633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:22:19  来源:igfitidea点击:

Problem with EditText background (android)

android

提问by Dennie

I have a problem with EditText background like this

我有这样的 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"      
      />

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

替代文字 http://i765.photobucket.com/albums/xx299/trieutrinhtrinh/edittext.jpg

After try to set the background, It look worse

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

<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

替代文字 http://i765.photobucket.com/albums/xx299/trieutrinhtrinh/edittext2.jpg

What's happen with EditText background? How to make EditText keep default style?

EditText 背景会发生什么?如何让 EditText 保持默认样式?

采纳答案by Bostone

If you set your EditText background to a color you will effectively suppress Android's default background which is probably a Nine Patchbut definetely not just a simple color. As result - you will get a simplest form of EditText - a square box. Here's slightly outdated list of built-in drawablesto give you some idea

如果您将 EditText 背景设置为一种颜色,您将有效地抑制 Android 的默认背景,该背景可能是九个补丁,但绝对不仅仅是一种简单的颜色。结果 - 您将获得最简单的 EditText 形式 - 一个方形框。这里有一些有点过时的内置可绘制列表,可以给你一些想法

回答by NguyenDat

Here is 2 Solution to change background of EditText i have investigate before, hope it can help you:

这是我之前调查过的更改 EditText 背景的 2 解决方案,希望它可以帮助您:

Issue: When set Background to EditText it look so terrible

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

Analysys: EditText used ninepath image for background. Their used a selector to change background image base on current state of EditText (Enable/Focus/Press, Default)

Analysys: EditText 使用ninepath 图像作为背景。他们使用选择器根据 EditText 的当前状态更改背景图像(启用/聚焦/按下,默认)

There are two solution to solver this problem, each solution have both advantage and disadvantaged:

有两种解决方案来解决这个问题,每个解决方案都有优点和缺点:

Solution1: Create custom yourself EditText (follow this solution we have freely change view of EditText.

解决方案1:自己创建自定义EditText(按照这个解决方案我们可以自由改变EditText的视图。

Advantage: Your freely render EditText view follow your purpose, No need to create Ninepath image as current implement of Android EditText. (Your must provider IF in your EditText to change background smoothly base on state of EditText (Enable/Focus....) Reused able and more custom in case you want to change color of Background or add more color

优点:你自由渲染的 EditText 视图符合你的目的,不需要像 Android EditText 的当前实现那样创建 Ninepath 图像。(您必须在 EditText 中提供 IF 才能根据 EditText 的状态(启用/聚焦...

Disadvantage: Take much effort to create and test your custom EditText. (I choose solution 2 so have no demo implement of solution 1, if any one follow this solution feel free to share with us your demo code)

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

Solution2: Used selector as Android implement

解决方案2:使用选择器作为Android工具

?Create xml file call edittext_selector.xml

?创建xml文件调用edittext_selector.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>

?On EditText xml set selector:

? 在 EditText xml 设置选择器上:

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

Note:

注意

● In this demo code i was remove some behavior of view state, refer android implement for detail behavior (focus/unfocus, enable/disable, press, selected ...)

● 在此演示代码中,我删除了视图状态的一些行为,请参阅 android 工具了解详细行为(聚焦/不聚焦、启用/禁用、按下、选择...)

● Take care order of item in your selector. Difference order of item in selector xml file will have difference background.

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

Advantage: Simple, just create selector and set selector to background, in case you want more color, just set more selector then set by code.

优点:简单,只需创建选择器并将选择器设置为背景,如果您想要更多颜色,只需设置更多选择器然后通过代码设置。

Disadvantage: Take effort to create ninepath image for selector, in case you want change color or add more color you must create more image and selector. So it less robust than Solution1

缺点:为选择器创建九路径图像需要努力,如果您想更改颜色或添加更多颜色,则必须创建更多图像和选择器。所以它不如解决方案1健壮

This is my investigate to handler background of image, so it may right or wrong, if you have better solution or explain, feel free to share with us.

这是我对图像处理程序背景的调查,因此可能对或错,如果您有更好的解决方案或解释,请随时与我们分享。

I was implement follow solution 2 and it worked.

我正在实施遵循解决方案 2 并且它起作用了。

回答by Hugo Sepulveda

My solution is a single line of code:

我的解决方案是一行代码:

<your-widget-component-that-has-a-background-color>.getBackground().setColorFilter(Color.<your-desired-color>, PorterDuff.Mode.MULTIPLY);).

It breaks down like this:

它分解如下:

  • "getBackground()" fetches the background from the component
  • "setColorFilter" will call a filtering on the background image itself
  • "Color.<your-color-here>" determines what color you want to pass onto the filter
  • "PorterDuff.Mode.<your-desired-filter-mode>" sets the kind of manipulation you would like to do with the given color and the fetched background image.
  • "getBackground()" 从组件中获取背景
  • “setColorFilter”将对背景图像本身调用过滤
  • "Color.<your-color-here>" 决定了你想传递给过滤器的颜色
  • "PorterDuff.Mode.<your-desired-filter-mode>" 设置您想要对给定颜色和获取的背景图像进行的操作类型。

People with knowledge of image editing software might recognise the mode. Each mode has a certain effect on how the color is applied to the background image. To simply "override" the color of the image, while preserving its gradients, borders and such, use MULTIPLY.

了解图像编辑软件的人可能会认出该模式。每种模式对颜色如何应用于背景图像都有一定的影响。要简单地“覆盖”图像的颜色,同时保留其渐变、边框等,请使用MULTIPLY.

回答by Fred

If you wish to edit the color of the Android background on the fly without changing the background image completely, try the following: (it is probably not the best solution but it works):

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

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

回答by Bernard Banta

You don't need to create the image. There is a built in image in the android system that you can use.So edit your EditText in xml as following;-

您不需要创建图像。您可以使用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>

Note this line : android:background="@android:drawable/editbox_background_normal"

请注意这一行: android:background="@android:drawable/editbox_background_normal"

回答by Shtirlic

As I think you should change background Color, not the background. Because it's using xml custom shape.

我认为您应该更改背景颜色,而不是背景。因为它使用 xml 自定义形状。

  • A drawable to use as the background. This can be either a reference to a full drawable resource (such as a PNG image, 9-patch, XML state list description, etc), or a solid color such as #ff000000(black).

  • May be a reference to another resource, in the form @[+][package:]type:nameor to a theme attribute in the form ?[package:][type:]name.

  • May be a color value, in the form of #rgb, #argb, #rrggbb, or #aarrggbb.

  • 用作背景的可绘制对象。这可以是对完整可绘制资源(例如 PNG 图像、9-patch、XML 状态列表描述等)的引用,也可以是纯色,例如#ff000000(黑色)。

  • 可能是对另一个资源的引用,在表单中@[+][package:]type:name或对表单?[package:][type:]name 中的主题属性的引用。

  • 可以是彩色值,在形式#rgb#argb#rrggbb,或#aarrggbb

回答by Thomas Ahle

Check out http://www.androidworks.com/changing-the-android-edittext-ui-widgetif you want to style your EditText's.

如果您想设置 EditText 的样式,请查看http://www.androidworks.com/changed-the-android-edittext-ui-widget

回答by Barrie Galitzky

I had to use SRC_ATOP for it to work for me

我必须使用 SRC_ATOP 才能为我工作

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