在android中更改edittext的背景颜色

Nar*_*uto 28 android android-layout android-edittext

如果我EditText使用下面的代码更改我的背景颜色,它看起来像是缩小的框,并且它不保持默认存在的蓝色底部边框的ICS主题EditText.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#99000000"
    >
    <EditText
        android:id="@+id/id_nick_name"
        android:layout_marginTop="80dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"  
    />
    <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             android:layout_marginTop="10dip"
             android:layout_marginLeft="20dip"
             android:layout_marginRight="20dip"
            android:orientation="horizontal"
            android:layout_below="@+id/id_nick_name">  
        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="add"
            android:layout_weight="1"
            />
         <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="cancel"
            android:layout_weight="1"
            />
    </LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是它的样子:

EditText的图片

Bar*_*zky 39

一行惰性代码:

mEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)


小智 19

这是最好的方法

首先:xmlres/中drawable命名新文件rounded_edit_text然后粘贴:

<?xml version="1.0" encoding="utf-8"?>
<shape  xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="#F9966B" />
    <corners
        android:bottomRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

第二:在res/layout复制和过去的下面的代码(代码EditText)

<EditText
    android:id="@+id/txtdoctor"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/rounded_edit_text"
    android:ems="10" >
    <requestFocus />
</EditText>
Run Code Online (Sandbox Code Playgroud)


RN3*_*ick 11

我创建了color.xml文件,用于命名我的颜色名称(黑色,白色......)

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <color name="white">#ffffff</color>
    <color name="black">#000000</color>
 </resources>
Run Code Online (Sandbox Code Playgroud)

在EditText中,设置颜色

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdsadasdasd"
        android:textColor="@color/black"
        android:background="@color/white"
        />
Run Code Online (Sandbox Code Playgroud)

或者在style.xml中使用样式:

<style name="EditTextStyleWhite" parent="android:style/Widget.EditText">
    <item name="android:textColor">@color/black</item>
    <item name="android:background">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并将ctreated样式添加到EditText:

 <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdsadasdasd"
        style="@style/EditTextStyleWhite"
        />
Run Code Online (Sandbox Code Playgroud)


Bas*_*rif 9

您应该做的是为edittext创建9补丁图像并将该图像设置为编辑文本背景.您可以使用网站创建9个补丁

我附上了一个样本9补丁图像供您参考.使用它作为编辑文本背景,你会得到一个想法.右键单击图像并选择"将图像另存为".保存图像时别忘了将其扩展名设为"9.png"

在此输入图像描述