将样式应用到 TextInputLayout 时应用程序崩溃

Ank*_*oor 1 android android-textinputlayout

当我在 TextInputLayout 中使用样式时,出现以下错误。我怎样才能解决这个问题?

引起原因:java.lang.IllegalArgumentException:此组件上的样式要求您的应用程序主题为 Theme.MaterialComponents(或其后代)。

<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>
Run Code Online (Sandbox Code Playgroud)

应用此解决了问题,但在 TextInputLayout 中未实现轮廓边框

样式.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>
Run Code Online (Sandbox Code Playgroud)

小智 5

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

<style name="AppTheme.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        ...
 </style>
Run Code Online (Sandbox Code Playgroud)

例如上面的代码。因此将其更改为 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>
Run Code Online (Sandbox Code Playgroud)