自定义设计EditText

Dev*_*ath 43 android android-xml android-edittext

我有自定义设计 EditText

在此输入图像描述

search_page.xml

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    android:background="#E1E1E1"
    android:weightSum="1" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:text="City" />

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="@drawable/rounded_edittext"
        android:layout_weight=".75" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

rounded_edittext.xml

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

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

    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

</shape>
Run Code Online (Sandbox Code Playgroud)

我想使用颜色代码#2f6699获取边框颜色,如EditText文本框中的轮廓,如下所示:

在此输入图像描述

关于如何实现这一点的任何想法?

Man*_*ika 70

rounded_edittext.xml中使用以下代码

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

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

    <stroke
        android:width="1dp"
        android:color="#2f6699" />
    <corners 
        android:radius="10dp"            
        />

</shape>
Run Code Online (Sandbox Code Playgroud)

这应该工作

  • 笔画=边框,实心=背景.我不知道为什么谷歌重新发明了轮子. (4认同)

Faa*_*hir 20

在此输入图像描述

对于上图中的EditText,您必须在res - > drawable文件夹中创建两个xml文件.首先将" bg_edittext_focused.xml "粘贴到其中的代码行

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <solid android:color="#FFFFFF" />
        <stroke
            android:width="2dip"
            android:color="#F6F6F6" />
        <corners android:radius="2dip" />
        <padding
            android:bottom="7dip"
            android:left="7dip"
            android:right="7dip"
            android:top="7dip" />
    </shape>
Run Code Online (Sandbox Code Playgroud)

第二个文件将" bg_edittext_normal.xml "粘贴到其中的代码行

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <solid android:color="#F6F6F6" />
        <stroke
            android:width="2dip"
            android:color="#F6F6F6" />
        <corners android:radius="2dip" />
        <padding
            android:bottom="7dip"
            android:left="7dip"
            android:right="7dip"
            android:top="7dip" />
    </shape>
Run Code Online (Sandbox Code Playgroud)

在res - > drawable文件夹中创建另一个名为" bg_edittext.xml "的xml文件,该文件将调用上述代码.将以下代码行粘贴到bg_edittext.xml中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_edittext_focused" android:state_focused="true"/>
    <item android:drawable="@drawable/bg_edittext_normal"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

最后在res - > layout - > example.xml文件中,无论你在哪里创建editText,你都会调用bg_edittext.xml作为背景

   <EditText
    :::::
    :::::  
    android:background="@drawable/bg_edittext"
    :::::
    :::::
    />
Run Code Online (Sandbox Code Playgroud)