Edittext使用shape.xml更改边框颜色

时间:2022-11-21 14:50:48

I make a shape.xml file under res -> drawable folder.

我在res - > drawable文件夹下创建了一个shape.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
        <solid android:color="#ffffff" />
        <stroke android:width="1dip" android:color="#ff9900" />
</selector>

And then I use it on an EditText:

然后我在EditText上使用它:

<EditText
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:id="@+id/editText"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="300dp"
    android:hint="@string/hint"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:singleLine="true"
    android:background="@drawable/shape"/>

But the result is that it doesn't change the border color at all. Why, what's wrong?

但结果是它根本不改变边框颜色。为什么,怎么了?

7 个解决方案

#1


72  

Why using selector as the root tag? selector is used for applying multiple alternate drawables for different states of the view, so in this case, there is no need for selector.

为什么使用selector作为根标记? selector用于为视图的不同状态应用多个备用drawable,因此在这种情况下,不需要选择器。

Try the following code.

请尝试以下代码。

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

    <!-- Background Color -->
    <solid android:color="#ffffff" />

    <!-- Border Color -->
    <stroke android:width="1dp" android:color="#ff9900" />

    <!-- Round Corners -->
    <corners android:radius="5dp" />

</shape>

Also It's worth mentioning that all color entries support alpha channel as well, meaning that you can have transparent or semi-transparent colors. For example #RRGGBBAA.

另外值得一提的是,所有颜色条目也支持alpha通道,这意味着您可以使用透明或半透明颜色。例如#RRGGBBAA。

#2


11  

Step 1:Create a border.xml in Drawable folder

第1步:在Drawable文件夹中创建border.xml

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

    <corners
        android:radius="2dp"
        />
    <solid android:color="#ffffff"
        />
    <stroke
        android:width="2dip"
        android:color="#000" />
</shape>

Step 2: Create a EditText in XML File

第2步:在XML文件中创建EditText

 <EditText
        android:id="@+id/etEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:hint="Enter Email"
        android:padding="10dp"
        android:layout_marginRight="25dp"
        android:background="@drawable/border"
        android:inputType="textEmailAddress"
        android:singleLine="true" />

#3


2  

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     android:shape="rectangle">
     <solid android:color="#ffffff" />
     <stroke android:width="1dip" android:color="#ff9900" />
 </selector>

You have to remove > this from selector root tag, like below

你必须从选择器根标签中删除> this,如下所示

   <selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

As well as move your code to shape from selector.

以及将代码从选择器移动到形状。

#4


1  

selector is used for applying multiple alternate drawables for different status of the view, so in this case, there is no need for selector

selector用于为视图的不同状态应用多个备用drawable,因此在这种情况下,不需要选择器

instead use shape

而是使用形状

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#ff9900" />
</shape>

#5


1  

i use as following for over come this matter

我使用以下来过来这件事

edittext_style.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="1dp"
        android:color="#c8c8c8"/>
<corners android:radius="0dp" />

And applied as bellow

并应用如下

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editTextName"
            android:background="@drawable/edit_text_style"/>

try like this..

试试这样..

#6


0  

Use root tag as shape instead of selector in your shape.xml file, and it will resolve your problem!

在shape.xml文件中使用root标签作为形状而不是选择器,它将解决您的问题!

#7


0  

Use this code on xml . i hope it will be work

在xml上使用此代码。我希望它会起作用

<?xml version="1.0" encoding="utf-8" ?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:thickness="0dp"
           android:shape="rectangle">
      <stroke android:width="3dp"
             android:color="#4799E8"/>
      <corners android:radius="5dp" />
      <gradient
       android:startColor="#C8C8C8"
       android:endColor="#FFFFFF"
       android:type="linear"
       android:angle="270"/>
    </shape>

#1


72  

Why using selector as the root tag? selector is used for applying multiple alternate drawables for different states of the view, so in this case, there is no need for selector.

为什么使用selector作为根标记? selector用于为视图的不同状态应用多个备用drawable,因此在这种情况下,不需要选择器。

Try the following code.

请尝试以下代码。

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

    <!-- Background Color -->
    <solid android:color="#ffffff" />

    <!-- Border Color -->
    <stroke android:width="1dp" android:color="#ff9900" />

    <!-- Round Corners -->
    <corners android:radius="5dp" />

</shape>

Also It's worth mentioning that all color entries support alpha channel as well, meaning that you can have transparent or semi-transparent colors. For example #RRGGBBAA.

另外值得一提的是,所有颜色条目也支持alpha通道,这意味着您可以使用透明或半透明颜色。例如#RRGGBBAA。

#2


11  

Step 1:Create a border.xml in Drawable folder

第1步:在Drawable文件夹中创建border.xml

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

    <corners
        android:radius="2dp"
        />
    <solid android:color="#ffffff"
        />
    <stroke
        android:width="2dip"
        android:color="#000" />
</shape>

Step 2: Create a EditText in XML File

第2步:在XML文件中创建EditText

 <EditText
        android:id="@+id/etEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp"
        android:hint="Enter Email"
        android:padding="10dp"
        android:layout_marginRight="25dp"
        android:background="@drawable/border"
        android:inputType="textEmailAddress"
        android:singleLine="true" />

#3


2  

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     android:shape="rectangle">
     <solid android:color="#ffffff" />
     <stroke android:width="1dip" android:color="#ff9900" />
 </selector>

You have to remove > this from selector root tag, like below

你必须从选择器根标签中删除> this,如下所示

   <selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

As well as move your code to shape from selector.

以及将代码从选择器移动到形状。

#4


1  

selector is used for applying multiple alternate drawables for different status of the view, so in this case, there is no need for selector

selector用于为视图的不同状态应用多个备用drawable,因此在这种情况下,不需要选择器

instead use shape

而是使用形状

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#ff9900" />
</shape>

#5


1  

i use as following for over come this matter

我使用以下来过来这件事

edittext_style.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="1dp"
        android:color="#c8c8c8"/>
<corners android:radius="0dp" />

And applied as bellow

并应用如下

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editTextName"
            android:background="@drawable/edit_text_style"/>

try like this..

试试这样..

#6


0  

Use root tag as shape instead of selector in your shape.xml file, and it will resolve your problem!

在shape.xml文件中使用root标签作为形状而不是选择器,它将解决您的问题!

#7


0  

Use this code on xml . i hope it will be work

在xml上使用此代码。我希望它会起作用

<?xml version="1.0" encoding="utf-8" ?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:thickness="0dp"
           android:shape="rectangle">
      <stroke android:width="3dp"
             android:color="#4799E8"/>
      <corners android:radius="5dp" />
      <gradient
       android:startColor="#C8C8C8"
       android:endColor="#FFFFFF"
       android:type="linear"
       android:angle="270"/>
    </shape>