首页 > 解决方案 > 将 Theme.DeviceDefault 与 AppCompatActivity 一起使用

问题描述

我有一个AppCompatActivity基于Theme.AppCompat. 我想创建一个相同应用程序的风格,但它使用Theme.DeviceDefault主题。当我更改主题时,我收到此错误:

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

我考虑用 替换所有实例AppCompatActivityFragmentActivity但这会导致很多功能中断。

有没有办法在使用的Theme.DeviceDefault应用程序中使用主题AppCompatActivity

标签: androidandroid-themeandroid-stylesillegalstateexception

解决方案


我不知道,但您可能需要考虑以下事项;

随着新版本平台的发布,例如根据源代码引入了新组件,似乎 Theme.AppCompat 将正确处理以前不可用的不同组件/资源的样式,并设置重要的东西,例如视图充气器类 <item name="viewInflaterClass">android.support.v7.app.AppCompatViewInflater</item>

这就是它使用 V7 基本主题作为其父主题的原因:

 <style name="Base.Theme.AppCompat" parent="Base.V7.Theme.AppCompat">    

您的另一个选择(我没有测试过)是深入了解您想要使用的主题中的资源并将它们全部复制,这当然很痛苦。

<!-- The default theme for apps that target API level XX and higher.
     <p>The DeviceDefault themes are aliases for a specific device’s native look and feel. The
     DeviceDefault theme family and widget style family offer ways for you to target your app
     to a device’s native theme with all device customizations intact.</p>
     <p>For example, when you set your app's {@code targetSdkVersion} to XX or higher, this
     theme is applied to your application by default. As such, your app might appear with the
     {@link #Theme_Material Material} styles on one device, but with a different set of styles on
     another device. This is great if you want your app to fit with the device's native look and
     feel. If, however, you prefer to keep your UI style the same across all devices, you should
     apply a specific theme such as {@link #Theme_Material Material} or one of your own design.
     For more information, read <a
     href="http://android-developers.blogspot.com/20XX/XX/material-everywhere.html">Material
     Everywhere</a>.</p>
     <p>Styles used by the DeviceDefault theme are named using the convention
     Type.DeviceDefault.Etc (for example, {@code Widget.DeviceDefault.Button} and
     {@code TextAppearance.DeviceDefault.Widget.PopupMenu.Large}).</p>
      -->

推荐阅读