简体   繁体   English

如何在Android中制作“ L”形的编辑文本可绘制选择器

[英]How to make a “L” shaped edit text drawable selector in android

I am trying to create a EditText as below 我正在尝试如下创建一个EditText

在此处输入图片说明

Question: 题:

  • How can i write a selector for this and set as background 我如何为此编写一个选择器并设置为背景
  • I do not want to place a image 我不想放置图像

You need to use a <layer-list> to accomplish what you want. 您需要使用<layer-list>完成所需的操作。 Taken from this answer 取自这个答案

res/drawable/text.xml: RES /绘制/ text.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#000000" />   
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#000000" />   
        </shape>
    </item>
    <item android:bottom="3px" android:left="3px">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />           
        </shape>
    </item> 
</layer-list>

res/layout/activity_main.xml: RES /布局/ activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<EditText
    android:id="@+id/editText1"
    android:background="@drawable/text"
    android:layout_width="170dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="30dp"
    android:ems="10"
    android:gravity="center_horizontal"
    android:padding="2dp"
    android:password="true" />
</RelativeLayout>

Expected Output: 预期产量:
在此处输入图片说明

This would work 这会工作

  1. Layout 布局

     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:ems="10" android:layout_margin="5dp" android:paddingLeft="5dp" android:hint="Email" android:background="@drawable/test_edit" android:id="@+id/editText" /> 

  2. Layer_list Layer_list

     <item android:drawable="@color/lightBlue"/> <item android:drawable="@color/darkBlue" android:left="2dp" android:bottom="2dp" /> 

  3. Color 颜色

     darkBlue #ff1ca9ff lightBlue #ffb6f1ff 

Not sure if this may help you but I would just create through the xml and use the Android 4.4 theme to style it L shaped. 不知道这是否对您有帮助,但我只是通过xml创建并使用Android 4.4主题将其设置为L形。 I would then set the background color to blue. 然后,我将背景色设置为蓝色。 This is how it done: 这是怎么做的:

Create the color.xml file, to enter the color values you need. 创建color.xml文件,以输入所需的颜色值。

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <color name="dodgerblue">#1E90FF</color>
    <color name="lightskyblue_1">#B0E2FF</color>
 </resources>

Then, in your EditText, set the color as follows 然后,在您的EditText中,如下设置颜色

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="asdsadasdasd"
    android:textColor="@color/lightskyblue_1"
    android:background="@color/dodgerblue"
/>

If you need more colors, here is a useful link. 如果您需要更多颜色, 是一个有用的链接。 Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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