Material布局默认边距问题:TextInputEditText设margin为0仍有内部间距
问题原因
你为TextInputLayout设置的样式是按钮专属样式@style/Widget.MaterialComponents.Button.OutlinedButton,该样式自带按钮适配的内部边距参数,和TextInputLayout的布局逻辑不匹配,这是出现额外默认边距的核心原因,和内部TextInputEditText的margin设置无关。
解决方案
- 第一步:将TextInputLayout的样式替换为官方提供的TextInputLayout专属描边样式:
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" - 第二步:如果需要自定义编辑区域和边框的间距,可给TextInputLayout添加
contentPadding相关属性直接调整内部边距,示例:app:contentPaddingStart="12dp" app:contentPaddingTop="8dp" app:contentPaddingEnd="12dp" app:contentPaddingBottom="8dp" - 第三步:如果不需要hint上浮的交互效果,可以添加
app:hintEnabled="false"关闭hint功能,可进一步去掉顶部预留的hint展示边距。
修正后的完整代码
<com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:id="@+id/textInputLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email Id" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" app:layout_constraintBottom_toTopOf="@+id/TXTpassword" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/IMGlogo"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/TXTemail" android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout>
内容的提问来源于stack exchange,提问作者Atharva Sabde
相关产品推荐
相关产品推荐

