【问题标题】:TextInputLayout Material Component Issue : The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant)TextInputLayout 材质组件问题:此组件上的样式要求您的应用主题为 Theme.MaterialComponents(或后代)
【发布时间】:2021-06-18 19:26:40
【问题描述】:

我在使用 TextInputLayout 实现 Material 设计时遇到问题,我知道解决方案是让您的活动主题继承 Material Component Theme 之一,但这会给我的大多数应用程序主题带来这样的变化,并且需要更多时间来重构它。我想要的是专门针对应用程序上这个特定的 TextInputLayout 的材料设计。

这是我尝试过的

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/firstName"
                style="@style/CustomInputStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="5dp"
                android:layout_weight="1"
                app:errorEnabled="true">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/fname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/first_name"
                    android:inputType="text|textCapWords"
                    android:nextFocusDown="@id/lname"
                    android:textSize="12sp" />

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




<style name="CustomInputStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
            <item name="boxStrokeWidth">2dp</item>
            <item name="hintTextColor">@color/colorAccent</item>
            <item name="boxStrokeColor">@android:color/white</item>
            <item name="android:colorAccent">@color/colorAccent</item>
            <item name="android:textColorHint">@color/colorAccent</item>
        </style>

错误: Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

我们不能只这样做而不是覆盖整个应用程序/活动主题吗?我只想在特定的 Fragment 上使用它。

【问题讨论】:

  • 使用桥梁主题

标签: android material-design android-theme material-components-android


【解决方案1】:

我只是注意到可以将 Material Components 主题放在 XML 布局的根视图中,然后您现在可以应用该样式而不会出现任何错误。

在父/根视图中添加这个

android:theme="@style/Theme.MaterialComponents.Bridge"

然后将样式应用到子视图TextInputLayout

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/firstName"
            style="@style/CustomInputStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_weight="1"
            app:errorEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/fname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/first_name"
                android:inputType="text|textCapWords"
                android:nextFocusDown="@id/lname"
                style="@style/CustomEditText"
                android:textSize="12sp" />

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

【讨论】:

    【解决方案2】:

    问题:如果您想对“TextInputLayout”应用错误的样式,有时会发生这种情况。

    例如:在我的情况下,我尝试使用“TextInputLayout”作为下拉列表 所以在“TextInputLayout”里面我放了“AutoCompleteTextView”,在最后一个里面我确定了样式 style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu" 所以它给了我上面的错误。

    解决:将样式更改为正确的样式。 style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu" 它现在可以正常运行,并且应用程序运行正常。

    之前:
    style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"

    之后: style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 2019-04-27
      • 2021-05-09
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多