问题
I have an android screen which takes email from the user. Below is the snippet of the code, I want to remove the underline which appears below the text.
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="@string/email"
android:textColorHint="@color/blueBackground"
app:boxBackgroundColor="@color/input_background"
app:boxCornerRadiusBottomEnd="20dp"
app:boxCornerRadiusBottomStart="20dp"
app:boxCornerRadiusTopEnd="20dp"
app:boxCornerRadiusTopStart="20dp"
app:endIconMode="clear_text"
app:endIconTint="@color/blueBackground"
app:hintTextColor="@color/blueBackground">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/blueBackground"
android:textSize="18sp"
android:layout_margin="8dp" />
</com.google.android.material.textfield.TextInputLayout>
I have tried android:background="@null"
and
<item name="colorControlNormal">@android:color/transparent</item>
<item name="colorControlActivated">@android:color/transparent</item>
But it is not working.
回答1:
It looks like the Material Components library draws its own underline using app:boxStrokeColor
. This attribute is a ColorStateList, so you have to create a color state list resource in which all states' colors are set to transparent. So basically you want to create a new file res/color/filename.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="@android:color/transparent"
android:state_pressed="true"
android:state_focused="true"
android:state_selected="true"
android:state_checkable="true"
android:state_checked="true"
android:state_enabled="true"
android:state_window_focused="true" />
</selector>
and set the app:boxStrokeColor
attribute to @color/filename
.
However, this left me with a black underline which still doesn't respond to android:background=@null
or <item name="colorControl*">@android:color/transparent</item>
.
TL;DR
Quickfix: if you set the TextInputLayout
outlined using style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
, the box looks almost the same, but the underline is gone. In order to remove the stroke just set the app:boxStrokeColor
attribute to @null
.
回答2:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="textInputStyle">@style/Widget.MyApp.TextInputLayout</item>
</style>
<style name="Widget.MyApp.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="boxStrokeWidth">0dp</item>
<item name="boxStrokeWidthFocused">0dp</item>
</style>
回答3:
app:boxBackgroundMode="none"
in parent TextInputLayout
回答4:
Setting the background of the TextInputLayout to a drawable and the one of the TextInputEditText to transparent removes the underline:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="@string/email"
android:background="@drawable/blue_drawable"
app:endIconMode="clear_text"
app:endIconTint="@color/black"
app:hintTextColor="@color/black">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:background="@android:color/transparent"
android:textSize="18sp"
android:layout_margin="8dp" />
</com.google.android.material.textfield.TextInputLayout>
blue_drawable.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp"/>
<solid android:color="@android:color/holo_blue_bright"/></shape>
回答5:
Create a selector at res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.01" android:color="?attr/background" android:state_focused="true"/>
<item android:alpha="0.01" android:color="?attr/background" android:state_hovered="true"/>
<item android:alpha="0.01" android:color="?attr/background" android:state_enabled="false"/>
<item android:alpha="0.01" android:color="?attr/background"/>
</selector>
background here - any color that is close to you background.
Way 1
Then set it to your TextInputLayout
:
<com.google.android.material.textfield.TextInputLayout
...
app:boxStrokeColor="@drawable/text_input_selector"
...
Way 2, trough styles:
styles.xml:
<style name="vm_textFilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="boxStrokeColor">@drawable/text_input_selector</item>
</style>
<com.google.android.material.textfield.TextInputLayout
...
style="@style/vm_textFilledBox"
回答6:
If you want app:boxBackgroundMode with filled and without underline than this will work
<item name="boxStrokeWidthFocused">0dp</item>
<item name="boxStrokeWidth">0dp</item>
回答7:
Try setting background to @null
or @android:color/transparent
回答8:
Make your custom TextInputLayout like this
package com.google.android.material.textfield
import android.content.Context
import android.util.AttributeSet
class TextInputLayoutWithoutUnderline @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {
override fun updateEditTextBackground() {
// XXX don't call super on purpose
}
}
回答9:
Easy way;
app:boxBackgroundMode="filled"
app:boxStrokeWidth="0dp"
回答10:
Adding this line on TextInputLayout
works for me:
app:boxStrokeWidth="0dp"
UPDATE
Inside color res directory add et_box_color.xml file and add below lines inside of it:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="@color/transparent"
android:state_pressed="true"
android:state_focused="true"
android:state_selected="true"
android:state_checkable="true"
android:state_checked="true"
android:state_enabled="true"
android:state_window_focused="true"/>
</selector>
Now add Your Material Edit text like below:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
app:boxStrokeColor="@color/et_box_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/llBank">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="#00FFFFFF"
android:gravity="center"
android:hint="Amount"
android:imeOptions="actionDone"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
I added a background to make a border of the TextInputLayout, remove background of TextInputLayout if you don't need. Background of TextInputEditText will be necessary to make background transparent.
来源:https://stackoverflow.com/questions/57063519/remove-underline-from-textinputedittext