简体   繁体   English

仅在EditText中使用的DrawableStart图标下方删除下划线

[英]Remove underline only below DrawableStart icon used in EditText

在此处输入图片说明

I had a TextInputLayout where I set DrawableStart attribute which shows icon above underline of EditText but i don't need underline only below drawable. 我有一个TextInputLayout,在其中设置DrawableStart属性,该属性在EditText下划线上方显示图标,但我不需要仅在drawable下方的下划线。 Is there any way to do this by Customising TextInputEditText? 有什么办法可以通过自定义TextInputEditText来做到这一点?

Define your icon in ImageView and then define EditText to right of this icon. ImageView定义您的图标,然后在该图标的右侧定义EditText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_icon" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="textPassword"/>

    </android.support.design.widget.TextInputLayout>

</LinearLayout>

You are going at it the wrong way. 您正在以错误的方式进行操作。 The underline will always include all TextInputLayout since the underline is part of it and not part of the image. 该下划线将始终包含所有TextInputLayout,因为该下划线是它的一部分,而不是图像的一部分。 To achieve what you want to create a LinarLay like this: 为了实现您想要创建这样的LinarLay的目标:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<ImageView
    android:id="@+id/imageView"
    android:layout_width="88dp"
    android:layout_height="88dp"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_menu_camera"
    android:background="@drawable/bgXMLFileName"/>

<EditText
    android:inputType="text"
    android:id="@+id/some_text"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:textAppearance="?android:textAppearanceMedium"
    android:textColor="#000000"
    />
</LinearLayout>

Now create another layout xml file "bgXMLFileName" 现在创建另一个布局xml文件“ bgXMLFileName”

<?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="#000000" />
    <padding android:bottom="1dp" />
</shape>

Should do the job 应该做的工作

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_lock_black_24dp" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="password" />

    </android.support.design.widget.TextInputLayout>

</LinearLayout>

result: 结果: 在此处输入图片说明

Try this: 尝试这个:

if you don't need underline only below drawable. 如果您只需要在drawable下面加下划线。 than you can use background null in both case TextInputEditText and Edit Text like this 比这样您可以在两种情况下都使用background null的情况:TextInputEditText和Edit Text

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">


<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

   <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/email_img"
        android:hint="Email"
        android:paddingLeft="5dp"
        android:drawablePadding="@dimen/margin_10"
        android:background="@null"
        android:inputType="textPassword"/>

</android.support.design.widget.TextInputLayout>


<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_marginTop="@dimen/margin_10"
    android:layout_height="wrap_content">

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/password_icon"
        android:hint="Password"
        android:paddingLeft="5dp"
        android:drawablePadding="@dimen/margin_10"
        android:background="@null"
        android:inputType="textPassword"/>

</android.support.design.widget.TextInputLayout>

Output: 输出:

在此处输入图片说明

I hope it helps you 希望对您有帮助

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

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