如何修复只能与 Android 中的 Theme.AppCompat 主题一起使用的 AppCompat 小部件

问题描述 投票:0回答:1

在我的应用程序中,我有后台服务,并且在该服务中我想显示MaterialButton
我写了下面的代码,但是启动服务后显示以下错误:

View class com.google.android.material.button.MaterialButton is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).

我的 XML 代码:

<com.google.android.material.button.MaterialButton
    android:id="@+id/gotoStepsBtn"
    style="@style/BlueGradientButton"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/_40mdp"
    android:layout_marginTop="@dimen/_10mdp"
    android:layout_marginEnd="@dimen/_10mdp"
    android:layout_marginBottom="@dimen/_5mdp"
    android:paddingHorizontal="@dimen/_25mdp"
    android:text="@string/seeTasks"
    android:textColor="@android:color/white"
    android:textSize="@dimen/_11font_mdp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/scrollLay" />

款式代码:

<style name="BlueGradientButton" parent="ColoredShadowDark">
    <item name="android:insetTop">0dp</item>
    <item name="android:insetBottom">0dp</item>
    <item name="backgroundTint">@null</item>
    <item name="android:background">@drawable/bg_rounded_blue_gradient</item>
    <item name="android:textSize">@dimen/_12font_mdp</item>
</style>

主题代码:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">

我该如何修复它?

android kotlin
1个回答
0
投票

我遇到了类似的问题,并通过将主题添加到按钮的 xml 代码中来修复它。

android:theme="@style/Theme.AppCompat"
© www.soinside.com 2019 - 2024. All rights reserved.