简体   繁体   中英

Custom Background for editText in android

在此处输入图像描述

<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    <solid android:color="#FFFFFF"/>
    <stroke
    android:width="1.5dp"
    android:color="#E53935"
        />

        </shape>

I want to make this type of custom design for editText.

Try this:

在此处输入图像描述

Your custom file is perfect just you need to use it in background of edittext

<LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ffffff"
        xmlns:android="http://schemas.android.com/apk/res/android">


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="User name"
            android:padding="15dp"
            android:layout_margin="10dp"
            android:background="@drawable/et_cust"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:padding="15dp"
            android:layout_marginTop="20dp"
            android:layout_margin="10dp"
            android:background="@drawable/et_cust"/>


    </LinearLayout>

et_cust.xml

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

    <solid android:color="#FFFFFF"/>

    <stroke
        android:width="2dp"
        android:color="#c9837f"/>

</shape>

Use this for EditText as android:background="@drawable/background_et"

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" /> 

    <stroke android:color="#FF0000"
        android:width="2dp"></stroke>

    <corners android:radius="0dp" />
</shape>

And this for button:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FF0000" /> 

    <stroke android:color="#FFFFFF"
        android:width="0dp"></stroke>

    <corners android:radius="0dp" />
</shape>

Add a Drawable file custom_style.xml into your Drawable folder -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <stroke
        android:width="1dp"
        android:color="#000000" />

</shape>

And then use this as background to the EditText.

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Your EditText"
        android:padding="10dp"
        android:background="@drawable/edit_text_bg"/>

all you have to do is set stroke to your background.!
background_stroke.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
        <solid android:color="@android:color/white" />
        <stroke android:width="1dp" android:color="#E53935"/>
    </shape>

add this to xml:

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/background_stroke"/>

Insert your drawable as background in your xml file:

android:background="@drawable/your_drawable"
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="0.2dp" android:color="#FF0000" />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
</shape>

Set your background to edittext.

As you created this xml for edittext

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <stroke
        android:width="1dp"
        android:color="#ED1C22" />

</shape>

now you need to use this xml into EditText background like this.

android:background="@drawable/edit_text_bg"

and you final activity xml will be

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text_bg"
        android:hint="User name"
        android:paddingBottom="8dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="8dp"
        android:textColor="@color/black"
        android:textSize="14dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@drawable/edit_text_bg"
        android:hint="Password"
        android:paddingBottom="8dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="8dp"
        android:textColor="@color/black"
        android:textSize="14dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ED1C22"
        android:layout_marginTop="8dp"
        android:text="LOGIN"
        android:textColor="@color/white"/>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM