将Theme.DeviceDefault与AppCompatActivity结合使用

时间:2018-10-23 18:21:30

标签: android android-theme android-styles

我有一个用mockService.Setup(x => x.MethodA()).CallBase();实现的应用程序和一个基于AppCompatActivity的主题。我想创建相同应用程序的样式,但是使用Theme.AppCompat主题。更改主题时,出现此错误:

Theme.DeviceDefault

我曾考虑将java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 的所有实例替换为AppCompatActivity,但这会导致很多功能中断。

在使用FragmentActivity的应用程序中是否可以使用Theme.DeviceDefault主题?

1 个答案:

答案 0 :(得分:0)

不是我所知道的,但是您可能需要考虑以下内容;

随着平台新版本的发布,例如,按照源代码引入了新的组件,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>
      -->