Create a custom background drawable for a EditText

孤者浪人 提交于 2019-11-28 16:32:23

问题


I'm trying to set a custom drawable to a EditText as expected below:

So I wrote this custom drawable:

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

        android:paddingBottom="12dp"
        android:paddingLeft="36dp"
        android:paddingRight="12dp"
        android:paddingTop="12dp">
<item>
    <shape>
        <corners android:radius="6dp"/>
        <solid android:color="@android:color/white"/>
        <stroke
            android:width="1dp"
            android:color="#BBBBBB"/>
    </shape>
</item>
<item
    android:width="32dp"
    android:gravity="left">
    <shape android:shape="rectangle">
        <size android:width="32dp"/>
        <corners
            android:bottomLeftRadius="6dp"
            android:topLeftRadius="6dp"/>
        <solid android:color="#AAAAAA"/>
    </shape>
</item>
<item android:left="8dp">
    <bitmap
        android:gravity="left"
        android:src="@drawable/ic_email"
        android:tileMode="disabled"/>
</item>
</layer-list>

The result in preview is quite good (except different corner size, not handled by Android Studio)

But in device, it totally not works... The second item is stretched and does not respect the width attribute.

I know, I can do it with a 9-patch, but I want to know if is it possible with drawables?


回答1:


Managed to do it with a single background drawable and a compound drawable on the EditText.

bg_edittext_icon.xml

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

    <item>
        <shape>
            <corners android:radius="6dp" />
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#AAAAAA" />
            <corners android:radius="6dp" />
        </shape>
    </item>
    <item
        android:left="34dp">
        <!-- left is icon size + 2x side padding around icon-->
        <!-- 18 + 8 + 8 -->
        <shape>
            <solid android:color="@android:color/white" />
            <corners android:radius="6dp"
                android:topRightRadius="6dp"
                android:topLeftRadius="0dp"
                android:bottomRightRadius="6dp"
                android:bottomLeftRadius="0dp"/>
        </shape>
    </item>
    <item>
        <shape>
            <corners android:radius="6dp" />
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="1dp"
                android:color="#BBBBBB" />
        </shape>
    </item>
</layer-list>

In your layout, use it like that:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_edittext_icon"
    android:inputType="textEmailAddress"
    android:drawableLeft="@drawable/ic_email_white_18dp"
    android:drawablePadding="20dp"
    android:paddingLeft="8dp"
    android:paddingRight="12dp"
    android:paddingTop="12dp"
    android:paddingBottom="12dp"
    android:hint="Email"
    />

With paddingLeft the icon padding inside the grey box and drawablePadding the padding inside the grey box + padding of the edit zone.

Here is the result from Genymotion (aka, real device):

Hope you like it !



来源:https://stackoverflow.com/questions/32589596/create-a-custom-background-drawable-for-a-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!