EditText后台(android)存在问题

EditText后台(android)存在问题,android,Android,我有一个编辑文本背景这样的问题 <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="35sp" android:singleLine="true" android:lay

我有一个编辑文本背景这样的问题

<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"      
      />

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

<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"    
      />


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

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

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

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

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


如果您将EditText背景设置为一种颜色,您将有效地抑制Android的默认背景,但它肯定不仅仅是一种简单的颜色。因此,您将得到一个最简单的EditText形式—一个方形框。这里有点过时,可以给你一些想法

如果你想设计你的EditText的样式,请查看。

这里有两个解决方案,可以改变我以前调查过的EditText的背景,希望它可以帮助你:

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

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

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

解决方案1: 创建自定义EditText(按照此解决方案,我们可以自由更改EditText的视图

优势: 您的免费呈现EditText视图符合您的目的,无需创建Ninepath图像作为Android EditText的当前实现。(如果您的EditText中有您的提供商,则必须根据EditText的状态平滑地更改背景(启用/聚焦…) 如果您想更改背景颜色或添加更多颜色,则可重复使用,且更加自定义

劣势: 花很多精力来创建和测试您的自定义编辑文本。 (我选择解决方案2,因此没有解决方案1的演示工具,如果有人遵循此解决方案,请随时与我们分享您的演示代码)

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

❶创建xml文件调用edittext\u 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>

❷在EditText xml集合选择器上:

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

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


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

<your-widget-component-that-has-a-background-color>.getBackground().setColorFilter(Color.<your-desired-color>, PorterDuff.Mode.MULTIPLY);).
.getBackground().setColorFilter(Color.,PorterDuff.Mode.MULTIPLY);)。
它分解如下:

  • “getBackground()”从组件获取背景
  • “setColorFilter”将调用对背景图像本身的过滤
  • “颜色”。确定要传递到过滤器上的颜色
  • “PorterDuff.Mode.”设置要对给定颜色和获取的背景图像进行的操作类型

熟悉图像编辑软件的人可能会识别该模式。每种模式对背景图像的颜色应用方式都有一定的影响。要简单地“覆盖”图像的颜色,在保留其梯度、边框等的同时,使用
乘法

您不需要创建图像。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\u background\u normal”
我必须使用SRC\u-top才能让它为我工作

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

是不是android:textAppearance=“@android:attr/textAppearanceSmall”事实上-文本的语法是正确的,请参见下面我的答案。他用于背景色的语法是validokay关于语法,我认为他用阴影等覆盖默认的背景xml形状,并将其设置为纯色。你没有抓住要点吗?当你设置编辑文本的背景色时,内置的可绘制图形将被覆盖。除非如果你已经将你的EditText主题从默认的drawable中移除,那么就没有理由使用“android:backgroun=“@android:drawable/editbox\u background\u normal”这正是我想要的。谢谢!
mEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);