android中的editText的自定义背景

问题描述 投票:2回答:7

enter image description here

<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>

我想为editText制作这种类型的自定义设计。

android android-layout material-design
7个回答
1
投票

将drawable作为背景插入xml文件中:

android:background="@drawable/your_drawable"

1
投票

你所要做的就是将笔画设置为你的背景。 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>

将此添加到xml:

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

1
投票

为edittext创建此xml时

<?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>

现在你需要像这样使用这个xml进入EditText背景。

android:background="@drawable/edit_text_bg"

而你最终的活动xml将是

<?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>

1
投票

将此用于EditText作为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>

这个按钮:

<?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>

1
投票

将Drawable文件custom_style.xml添加到Drawable文件夹中 -

<?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>

然后使用它作为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"/>

1
投票
<?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>

将您的背景设置为edittext。


1
投票

试试这个 :

enter image description here

您的自定义文件非常完美,只需要在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>
© www.soinside.com 2019 - 2024. All rights reserved.