Android 中的 TextInputLayout 总是有一个默认背景

问题描述 投票:0回答:11
android android-layout android-textinputlayout android-textinputedittext
11个回答
77
投票

https://github.com/material-components/material-components-android/issues/102

这似乎是一个实质性的文本字段错误。
您可以使用此代码暂时更改背景白色。

<com.google.android.material.textfield.TextInputLayout
   app:boxBackgroundColor="@android:color/transparent" 
   android:background="@android:color/transparent">
   <com.google.android.material.textfield.TextInputEditText/>
</com.google.android.material.textfield.TextInputLayout>

40
投票

去除背景色的一种方法是调整TextInputLayout的背景模式。

应用程序:boxBackgroundMode =“无”

为我工作。

XML 代码示例:

<com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:boxBackgroundMode="none"
            app:errorEnabled="true"
            app:hintEnabled="false"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top"
                android:hint="@string/comment_placeholder"
                android:inputType="textCapSentences|textMultiLine"
                android:maxLength="500" />

        </com.google.android.material.textfield.TextInputLayout>

16
投票

方法A

(完全透明背景)

<com.google.android.material.textfield.TextInputLayout
    app:boxBackgroundMode="filled"
    app:boxBackgroundColor="@null"
..>

<com.google.android.material.textfield.TextInputEditText
    android:backgroundTint="@android:color/transparent"
..>

方法B

(纯色背景)

<com.google.android.material.textfield.TextInputLayout
    app:boxBackgroundMode="filled"
    app:boxBackgroundColor="?android:attr/colorBackground"
..>

*对于我的情况,我需要它完全透明,因为屏幕背景不是纯色。如果您是这种情况,请使用方法A


8
投票

对我来说它与

app:boxBackgroundColor="@null"
一起工作,就像这样:

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:boxBackgroundColor="@null"
        android:hint="@string/description">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences" />

    </com.google.android.material.textfield.TextInputLayout>

5
投票

在我的情况下需要更改 TextInputLayout 和 TextInputEditText 中的背景

<com.google.android.material.textfield.TextInputLayout
    app:boxBackgroundColor="@color/transparent"
    android:background="@color/transparent"
    >
    <com.google.android.material.textfield.TextInputEditText
        android:background="@color/transparent"
        />
</com.google.android.material.textfield.TextInputLayout>

4
投票

简单的方法对我有用!

app:boxBackgroundMode="filled"
app:boxBackgroundColor="@color/white"

3
投票

我最近遇到了这个问题。上述解决方案均不适用于最新的材质主题版本。我花了一段时间才弄清楚。这是我的解决方案:

风格:

<style name="Widget.App.TextInputLayout" parent="Widget.Design.TextInputLayout">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.TextInputLayout</item>
    <item name="hintTextColor">@color/pink</item>
    <item name="android:textColorHint">@color/grey</item>
</style>

<style name="ThemeOverlay.App.TextInputLayout" parent="">
    <item name="colorControlNormal">@color/grey</item>
</style>

用途:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/password"
        style="@style/Widget.App.TextInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        app:layout_constraintBottom_toTopOf="@+id/agreement"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"/>

</com.google.android.material.textfield.TextInputLayout>

我还强烈建议您查看这篇文章。 迁移至 Android 的材质组件


3
投票

默认背景颜色基于

colorOnSurface
属性。

您可以使用以下方法覆盖此值:

<com.google.android.material.textfield.TextInputLayout
   android:theme="@style/MyTextInputLayout_overlay"
   ..>

与:

<style name="MyTextInputLayout_overlay">
    <item name="colorOnSurface">@color/....</item>
</style>

或者您可以使用

boxBackgroundColor
属性(在布局或自定义样式中):

  <com.google.android.material.textfield.TextInputLayout
      app:boxBackgroundColor="@color/....."
      ..>

0
投票

一开始一切都很顺利。后来布局就被打破了。原因是我更改了属性背景TextInputEditText。透明度必须在 TextInputEditText 上设置,而不是在 TextInputLayout 中设置

android:background="#80FFFFFF"


0
投票

您可以使用这些选项来自定义 TextInputLayout。

  <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/menuMakeList"
                    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="20dp"
                    android:background="@drawable/background_round_menu_selector"
                    app:boxBackgroundColor="@android:color/transparent"
                    app:boxBackgroundMode="outline"
                    app:boxStrokeColor="@android:color/transparent"
                    app:boxStrokeWidth="0dp"
                    app:endIconDrawable="@drawable/ic_arrow_down"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/textViewHeader">

                    <AutoCompleteTextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="null"
                        android:hint="Make"
                        android:inputType="none"
                        android:padding="16dp"
                        app:hintTextColor="#C8CCC9" />

                </com.google.android.material.textfield.TextInputLayout>

事情就是这样。我已使用自定义圆角 xml 作为背景并覆盖材质 background_round_menu_selector.xml.

中的颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <corners android:radius="15dp" />
    <stroke
        android:width="2dp"
        android:color="#EAEAEA" />
</shape>

enter image description here


0
投票

有一种解决方法是在 com.google.android.material.textfield.TextInputEditText 中添加属性 android:paddingStart="48dp"。您也可以根据需要更改该值。

这是我的activity_main.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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mainbackground"
    android:orientation="vertical"
    tools:context=".SignUp">

   

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:startIconDrawable="@drawable/name"
        android:paddingStart="48dp"
        >

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="48dp"
            android:hint="Name" />

    </com.google.android.material.textfield.TextInputLayout>

       </LinearLayout>

Output of the Code

© www.soinside.com 2019 - 2024. All rights reserved.