Java 如何在EditText密码输入字段旁边放置ImageView

Java 如何在EditText密码输入字段旁边放置ImageView,java,android,xml,Java,Android,Xml,我正在尝试使用EditText TextView在密码字段中进行密码可见性切换(我不想使用android support TextInputLayout) 一切正常,但如何将ImageView调整到密码编辑文本字段旁边的正确位置 这就是我现在拥有的 这就是我想要的 我希望图像位于密码编辑文本字段中的那个位置 我的布局文件 <EditText android:layout_width="match_parent" android:layout_height="50dp"

我正在尝试使用EditText TextView在密码字段中进行密码可见性切换(我不想使用android support TextInputLayout)

一切正常,但如何将ImageView调整到密码编辑文本字段旁边的正确位置

这就是我现在拥有的

这就是我想要的

我希望图像位于密码编辑文本字段中的那个位置

我的布局文件

 <EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/etPassword"
    android:layout_marginRight="20dp"
    android:inputType="textPassword"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="20dp"
    android:paddingLeft="20dp"
    android:background="@drawable/input_border"
    android:hint="Your Password" />

<ImageView
    android:id="@+id/show_pass_btn"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp"
    android:alpha=".5"
    android:onClick="ShowHidePass"
    android:padding="5dp"
    android:src="@drawable/eye_show" />

我想建议两种解决方案。我自己也在用第二个

第一:通过向EditText添加可绘制的 您可以向编辑文本添加一个图标,如下所示:

    <EditText
        ...
        android:drawableEnd="@drawable/ic_settings_24dp"
        ...
    />

结果如下


您可以在
TextInputLayout
中使用
app:passwordToggleEnabled=“true”
,如果您还需要使用自定义图标,
“app:passwordToggleDrawable=“




我不想使用android支持文本InputLayout)
为什么?。这很简单。@IntelliJAmiya看起来与我喜欢的普通editText字段非常不同。我编写了java代码,只是想找到一种方法将图像放置在editText旁边field@Ibramazin您要查找的正是TextInputLayout。只需使用正确的样式TextInputLayout就可以实现这一功能不确定为什么要重新设计轮子-TextInputLayout做到了这一点是的,我仍在使用TextInputLayout,但它将默认的填充框样式应用于EditText。我不知道如何禁用该样式,所以我将TextInputLayout包装在自定义类中。这样AppTheme就不会向其添加材质样式。没有理由以这种方式扩展TextInputLayout。只需更改样式。尝试了TextInputLayout的所有可用样式,但似乎都不起作用。你知道哪种风格会让旧的EditText设计回归吗?虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题会真正有助于提高你的文章质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
import android.content.Context
import android.util.AttributeSet
import com.google.android.material.textfield.TextInputLayout

class CustomTextInputLayout @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): TextInputLayout(context, attrs, defStyleAttr) {

}
    <com.passwordstore.android.ui.views.CustomTextInputLayout
        android:id="@+id/til_notes"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/notes"
        app:endIconMode="clear_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn_generate_password"
        style="@style/BoxTextInputLayout">
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/poppins_medium" />
    </com.passwordstore.android.ui.views.CustomTextInputLayout>
      <com.google.android.material.textfield.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            app:passwordToggleEnabled="true">
        
                            <com.google.android.material.textfield.TextInputEditText
                                android:id="@+id/ed_password"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"  
                                android:gravity="center_vertical"
                                android:hint="Password"
                                android:imeOptions="actionNext"
                                android:inputType="textPassword"
                                android:textSize="16sp"
                                android:padding="10dp" />
        
        
                        </com.google.android.material.textfield.TextInputLayout>
<EditText
    android:id="@+id/loginPassword"
    android:layout_width="325dp"
    android:layout_height="53dp"
    android:layout_marginTop="20dp"
    android:background="@drawable/et_style"
    android:hint="Password"
    android:inputType="textPassword"
    android:paddingLeft="8dp"
    android:textColorHint="@color/colorOrange"
    android:textSize="20sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/loginUsername" />

<ImageView
    android:id="@+id/password_toggle_img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_marginEnd="12dp"
    android:layout_marginRight="12dp"
    app:layout_constraintBottom_toBottomOf="@id/loginPassword"
    app:layout_constraintEnd_toEndOf="@+id/loginPassword"
    app:layout_constraintTop_toTopOf="@+id/loginPassword"
    app:layout_constraintVertical_bias="0.0"
    app:srcCompat="@drawable/ic_baseline_visibility_24"
    tools:ignore="VectorDrawableCompat" />