TextInputLayout中Hint与startIconDrawable重叠问题求助
TextInputLayout提示文本与起始图标重叠问题解决
你的问题出在两个关键属性的设置上,直接修改即可解决:
移除
app:boxCollapsedPaddingTop="25dp"
这个属性会强制将折叠状态下的提示文本向下偏移,直接导致它和左侧的startIcon重叠。Material Design组件默认会自动处理提示文本与图标的间距,无需手动添加这个内边距。替换固定高度
android:layout_height="60dp"为wrap_content
固定高度会压缩TextInputLayout的内部空间,让提示文本和图标没有足够的空间分隔。使用wrap_content可以让控件根据内部元素自适应高度,保证布局正常。
修改后的完整XML代码:
<?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=".SignUp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sign Up" android:textStyle="bold" android:textSize="25sp" android:layout_margin="20dp" /> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:boxBackgroundColor="@color/white" app:startIconDrawable="@drawable/baseline_person_24" android:layout_marginStart="30dp" android:layout_marginEnd="30dp"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Name" /> </com.google.android.material.textfield.TextInputLayout> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Button" /> </LinearLayout>
另外补充:textSize建议用sp单位而非dp,这样会跟随系统字体大小变化,提升界面的可访问性。
内容的提问来源于stack exchange,提问作者Yaseen
相关产品推荐
相关产品推荐

