如何改变editText背景色(Android)?

时间:2015-11-17 11:54:56

标签: android

我试过了:

rounded_textfield.xml:

public static class VisualTreeExtensions
{
     public static T GetVisualParentOfType<T>(this DependencyObject child)
        where T : DependencyObject
    {
        if (child is T) 
            return child as T;
        var parentObject = VisualTreeHelper.GetParent(child);
        if (parentObject == null) return null;
        var parent = parentObject as T;
        return parent ?? GetVisualParentOfType<T>(parentObject);
    }

    public static T GetChildOfType<T>(this DependencyObject depObj)
    where T : DependencyObject
    {
        if (depObj == null) return null;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null) return result;
        }
        return null;
    }
}

activity.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" android:padding="10dp">
    <solid android:color="#EEEEEE"/>
    <corners
            android:bottomRightRadius="8dp"
            android:bottomLeftRadius="8dp"
            android:topLeftRadius="8dp"
            android:topRightRadius="8dp"/>
</shape>

在编辑器中它是灰色的,但在手机上它是白色的。

3 个答案:

答案 0 :(得分:4)

您的代码是正确的,只需清理项目即可。有一件事你应该记住,根据不同的设备显示设置和显示屏幕,不同颜色的颜色会有所不同。

答案 1 :(得分:1)

试试这个,

<EditText
                    android:id="@+id/edtPassword"
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/rounded_corner"
                    android:ems="10"
                    android:hint="Enter Password"
                    android:inputType="textPassword"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textColorHint="@android:color/white"
                    android:textSize="16sp" />

<强> rounded_corner.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="@android:color/transparent" />

    <corners
        android:bottomRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"/>

    <stroke
        android:width="1dp"
        android:color="@android:color/white" />

    <padding
        android:bottom="1dip" />

</shape>

答案 2 :(得分:0)

创建一个名为custom_edit_text.xml的文件,并将其放在项目res文件夹的xml文件夹中。

将以下代码粘贴在那里:

dispatch_semaphore_wait(dataSemaphore, DISPATCH_TIME_FOREVER)

现在只需在您的activty_main.xml文件中引用此xml,如下所示:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#ffffff"/>
</shape>

干杯!