You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何将包含TextInputEditText的TextInputLayout定制为普通EditText样式

让TextInputLayout和普通EditText样式完全一致的方案

我明白你想让密码输入框的外观和邮箱输入框完全对齐,这需要去掉TextInputLayout默认的Material样式,并且对齐所有文本和布局属性。下面是具体的修改步骤和完整代码:


核心修改点:

  • 去掉TextInputLayout的默认边框/背景和悬浮提示空间
  • 对齐TextInputEditText与原EditText的字体、字号、高度等属性
  • 保留密码切换按钮但不破坏整体布局

修改后的完整代码:

<EditText
    android:id="@+id/email_edit_text"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_marginTop="55dp"
    android:fontFamily="@font/poppins"
    android:hint="@string/enter_email_or_username"
    android:textSize="12sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/login_signup_linear_layout" />

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/password_input_field"
    android:layout_width="200dp"
    android:layout_height="40dp" <!-- 和EditText高度一致,去掉默认悬浮提示的额外高度 -->
    android:layout_marginTop="16dp" <!-- 可根据需要调整和邮箱框的间距 -->
    app:boxBackgroundMode="none" <!-- 去掉默认的Material边框和背景 -->
    app:passwordToggleEnabled="true"
    app:passwordToggleTint="@color/your_tint_color" <!-- 可选:调整密码切换按钮的颜色,和你的主题匹配 -->
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/email_edit_text">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/password_edit_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" <!-- 填充TextInputLayout的高度 -->
        android:fontFamily="@font/poppins" <!-- 和EditText字体一致 -->
        android:textSize="12sp" <!-- 和EditText字号一致 -->
        android:hint="@string/password"
        android:inputType="textPassword"
        android:paddingVertical="0dp" <!-- 去掉默认内边距,确保文本垂直居中 -->
        android:gravity="center_vertical" /> <!-- 文本垂直居中,和EditText对齐 -->

</com.google.android.material.textfield.TextInputLayout>

关键属性解释:

  1. app:boxBackgroundMode="none":这是最关键的一步,它会移除TextInputLayout默认的圆角边框、下划线和背景,让它完全透明,只作为容器存在。
  2. 固定高度:把TextInputLayout的layout_height设为和EditText一样的40dp,同时让TextInputEditText的高度设为match_parent,这样输入框的整体高度就和邮箱框一致了。
  3. 字体与对齐:同步TextInputEditText的fontFamily、textSize,并设置gravity="center_vertical"确保文本垂直居中,和EditText的视觉效果完全对齐。
  4. 密码按钮调整:如果默认的密码切换按钮大小或颜色不合适,可以用passwordToggleTint调整颜色,或者用passwordToggleSize调整大小,避免破坏整体布局。

这样修改后,密码输入框就会和邮箱输入框的外观完全一致,同时还保留了密码可见切换的功能。

内容的提问来源于stack exchange,提问作者nass_b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 10:47:33