TextInputLayout
和 TextInputEditText
是 Android 开发中常用的组件,用于创建具有浮动标签效果的输入框。默认情况下,TextInputEditText
会有一个底部的下划线,这是其样式的一部分。如果你想要移除这个下划线,可以通过以下几种方法实现:
res/values/styles.xml
文件中创建一个新的样式,继承自 Widget.MaterialComponents.TextInputLayout
,并设置 boxStrokeWidth
为 0。res/values/styles.xml
文件中创建一个新的样式,继承自 Widget.MaterialComponents.TextInputLayout
,并设置 boxStrokeWidth
为 0。TextInputLayout
的 style
属性设置为刚刚创建的自定义样式。TextInputLayout
的 style
属性设置为刚刚创建的自定义样式。app:boxStrokeWidth
属性你也可以直接在布局文件中设置 app:boxStrokeWidth
属性为 0。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxStrokeWidth="0dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/textInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text" />
</com.google.android.material.textfield.TextInputLayout>
android:background
属性另一种方法是直接在 TextInputEditText
上设置 android:background
属性为 @null
。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/textInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text"
android:background="@null" />
</com.google.android.material.textfield.TextInputLayout>
android:background="@null"
可能会影响其他样式属性,因此建议优先使用前两种方法。implementation 'com.google.android.material:material:1.4.0' // 请使用最新版本
通过以上方法,你可以有效地移除 TextInputLayout
或 TextInputEditText
中的下划线。
领取专属 10元无门槛券
手把手带您无忧上云