应用程序在将样式应用于 TextInputLayout 时崩溃

App crashing on applying style to TextInputLayout

我在 TextInputLayout 中使用样式时出现以下错误。我该如何解决这个问题?

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

<com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:boxStrokeColor="#ffcc00"
        android:theme="@style/EditTextTheme"
        app:layout_constraintEnd_toStartOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@id/guidelineStart"
        app:layout_constraintTop_toTopOf="parent">

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

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

应用这个解决了问题,但在 TextInputLayout 中没有实现轮廓边框

styles.xml

<style name="EditTextTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

也许您的应用程序主题不是 material 主题

<style name="AppTheme.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        ...
 </style>

例如上面的代码。所以将其更改为 Theme.MaterialComponents.NoActionBar

但是如果您不想更改应用的孔主题,您可以将下面的代码添加到布局的根 ViewGroup

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/root_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.MaterialComponents.Bridge">
        
        ...

</androidx.constraintlayout.widget.ConstraintLayout>