gpt4 book ai didi

android - 无法在 AppCompatActivity 中使用 android :Theme. Material

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:03 24 4
gpt4 key购买 nike

首先我创建了两个样式,一个是value,一个是value-21。

在values/styles.xml中是

<resources>
<style name="AppTheme" parent="Theme.AppCompat"></style>
</resources>

在values-v21/styles.xml中是

<resources>
<style name="AppTheme" parent="android:Theme.Material"></style>
</resources>


我的gradle文件(app.gradle)

gradle pic1 gradle pic2


由于 ActionBarActivity 已弃用,所以我使用 AppCompatActivity

当我设置这两个样式值并在 MainActivity 中扩展 AppCompatActivity 时,出现异常:

java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity.

我做了两个不同的更改,但那个异常再也没有出现过:

  • 扩展“android.app.Activity”而不是 v7.AppCompatActivity。
  • 在 values-v21/styles.xml 中更改为“Theme.AppCompat”。


如果我想使用设计主题,是否应该扩展“android.app.Activity”?

或者,还有其他使用 Material 设计主题的方法吗?

谢谢。

最佳答案

在您的 style.xml 文件中写入以下主题。

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">any_color_code</item>
<item name="colorPrimaryDark">any_color_codek</item>
<item name="colorAccent">any_color_code</item>
</style>

并在你的`MainActivity 中写入 extends AppCompatActivity

并在 Manifest.xml 文件中编写您的应用程序标签,如下所示:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme">
</application>

关于android - 无法在 AppCompatActivity 中使用 android :Theme. Material,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41393461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com