如何在android中改变自定义形状的边界颜色?

时间:2022-11-21 12:34:35

I am creating an edit text with rounded corners, for which i am using a custom shape and assiging it to edit text. I want: 1- Filled area of edit text to be white. 2- Boundary of edit text should be green. the first requirement is fulfilled but i am unable to do the second one. how this can be achieved? any help will be appreciated Here is my code Edit text code:

我正在创建一个圆角的编辑文本,为此我使用一个自定义的形状并将其指定为编辑文本。我要:1-填充区域的编辑文本为白色。2-编辑文本边界应为绿色。第一个要求已经满足,但是我不能做第二个要求。如何实现这一点?如果您有任何帮助,欢迎在这里输入我的代码编辑文本代码:

<EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:hint="E-mailadres"
        android:background="@drawable/rounded_layout"
        android:layout_marginTop="8dp"
        android:layout_weight="2"/>

Custom layout:

自定义布局:

   <?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="#FFFFFF"/>
    <corners
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        />
    <stroke android:width="1dip"  />
</shape>

Image of required output: 如何在android中改变自定义形状的边界颜色?

图像所需的输出:

5 个解决方案

#1


1  

You need to add stroke to your to your drawable file.

您需要在您的drawable文件中添加stroke。

Replace code with this

用这个替换代码

<?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="#FFFFFF"/>
    <corners
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        />
    <stroke android:width="1dip" android:color="#0075cb"  />
</shape> 

#2


3  

Hello try below code with replace your stroke

你好,试试下面的代码替换你的笔划

<stroke android:width="1dip"
        android:color="**your color goes here**"/>

#3


1  

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="3dp" android:color="#8bc34a" />
    <corners android:radius="10dip"/>
    <gradient
        android:startColor="#ffffff"
        android:endColor="#ffffff"
        />
</shape>

#4


0  

Create an XML file put and it into drawable folder

创建一个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>

and Edittext code like this

以及这样的Edittext代码。

<EditText
            android:id="@+id/inputSearchEditText"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="15dp"
            android:layout_centerVertical="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="start"
            android:gravity="center"
            android:hint="Search text"
            android:inputType="text"
            android:background="@drawable/EditTextStyle"/>

#5


0  

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp"
    android:color="#04B404" />
<corners
    android:radius="3dp"/>
<solid android:color="#FFFFFF"/>
</shape>

add this in your drawable xml. and set this as background of your edittext.

将其添加到可绘制的xml中。并将其设置为edittext的背景。

#1


1  

You need to add stroke to your to your drawable file.

您需要在您的drawable文件中添加stroke。

Replace code with this

用这个替换代码

<?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="#FFFFFF"/>
    <corners
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        />
    <stroke android:width="1dip" android:color="#0075cb"  />
</shape> 

#2


3  

Hello try below code with replace your stroke

你好,试试下面的代码替换你的笔划

<stroke android:width="1dip"
        android:color="**your color goes here**"/>

#3


1  

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="3dp" android:color="#8bc34a" />
    <corners android:radius="10dip"/>
    <gradient
        android:startColor="#ffffff"
        android:endColor="#ffffff"
        />
</shape>

#4


0  

Create an XML file put and it into drawable folder

创建一个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>

and Edittext code like this

以及这样的Edittext代码。

<EditText
            android:id="@+id/inputSearchEditText"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="15dp"
            android:layout_centerVertical="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="start"
            android:gravity="center"
            android:hint="Search text"
            android:inputType="text"
            android:background="@drawable/EditTextStyle"/>

#5


0  

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp"
    android:color="#04B404" />
<corners
    android:radius="3dp"/>
<solid android:color="#FFFFFF"/>
</shape>

add this in your drawable xml. and set this as background of your edittext.

将其添加到可绘制的xml中。并将其设置为edittext的背景。