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

时间:2018-10-03 19:39:05

标签: android android-edittext

enter image description here

我有一个TextInputLayout,其中设置了DrawableStart属性,该属性在EditText下划线上方显示图标,但是我不需要仅在drawable下划线。有什么办法可以通过自定义TextInputEditText来做到这一点?

4 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

您正在以错误的方式进行操作。该下划线将始终包含所有TextInputLayout,因为该下划线是它的一部分,而不是图像的一部分。 要实现您想要的创建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>

现在创建另一个布局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>

应该去做

答案 2 :(得分:0)

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

结果: enter image description here

答案 3 :(得分:0)

尝试一下:

,如果您只需要在drawable下划线,比这样的情况下,在TextInputEditText和Edit Text情况下都可以使用background null

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

输出:

enter image description here

希望对您有帮助