如何移除TextInputEditText底部横线的左右内边距
问题根因
TextInputEditText底部横线的左右内边距不受android:padding属性控制,这部分缩进是TextInputLayout的Box组件内置的inset边距,单独给TextInputLayout、TextInputEditText设置padding="0dp"无法生效。
修复方案
直接在TextInputLayout控件上添加官方提供的inset控制属性,将边距置0即可:
<!-- 控制底部描边/下划线的左右缩进 --> app:boxStrokeInsetStart="0dp" app:boxStrokeInsetEnd="0dp" <!-- 如果设置后填充背景区域仍有左右缩进,补充以下两个属性 --> app:boxBackgroundInsetStart="0dp" app:boxBackgroundInsetEnd="0dp"
由于现有样式中配置了boxBackgroundColor属性,建议显式声明Box模式避免不同版本Material库的兼容问题:
app:boxBackgroundMode="filled"
修改后的TextInputLayout代码示例:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/ti_input_otp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="@string/zp_otpfragment_input_otp" android:padding="0dp" android:theme="@style/TextInputLayout" app:hintTextAppearance="@style/FloatingLabel" app:boxBackgroundMode="filled" app:boxStrokeInsetStart="0dp" app:boxStrokeInsetEnd="0dp" app:boxBackgroundInsetStart="0dp" app:boxBackgroundInsetEnd="0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv_input_otp_description"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/et_input_otp" android:layout_width="match_parent" android:layout_height="@dimen/text_input_layout_height" android:layout_marginTop="16dp" android:ems="10" android:gravity="start|center_vertical" android:imeOptions="actionNext" android:importantForAutofill="no" android:inputType="numberSigned" android:labelFor="@string/zp_otpfragment_input_otp" android:lines="1" android:padding="0dp" android:singleLine="true" android:textSize="@dimen/text_info_size" android:theme="@style/InputText" /> </com.google.android.material.textfield.TextInputLayout>
注意:以上属性在Material Components库1.2.0及以上版本均支持,不需要自定义背景Drawable,也不需要额外编写Java/Kotlin代码。
内容的提问来源于stack exchange,提问作者Víctor Martín
相关产品推荐
相关产品推荐

