为什么 Google 对 Material 设计的说明不正确?

Why are Google's instructions for Material Design incorrect?

我看到过类似的问题,解决方案解决了我的问题,但我想知道是否有人可以向我解释一下。

我正在按照 Google 开发者网站上的说明应用位于 here 的 Material 设计主题。但是,当我尝试 运行 模拟器上的应用程序时,我得到了一个 IllegalStateException。我的主题继承自 parent="android:Theme.Material",如 Google 提供的说明中所指定。

问题已通过使用 Theme.AppCompat 解决。为什么Google提供的指令会出错?我读到 Theme.AppCompat extends Theme.Material,我不知道这是不是真的。为什么不能Theme.Material不能用?

我尝试遵循 Theme.AppCompat 的扩展行,但我没有看到它继承自 Theme.Material 的任何地方。

Why is it that the instructions that Google provide leads to an error?

这些说明假定您使用 Activity,而不是 AppCompatActivity 作为活动的基础 class。在您的情况下,您使用的是 AppCompatActivity.

I read that Theme.AppCompat extends Theme.Material, I don't know if that is true.

这不是真的。

在 Android 5.0+ 设备上,Theme.AppCompat 将间接扩展 Theme.Material。在旧设备上,它不会,因为 Theme.Material 不存在。您的错误来自 AppCompatActivity,这表明您使用的主题基于 Theme.AppCompat,因此不接受 Theme.Material.

您可能遇到以下两个问题中的一个(或全部):

1) 您只使用了 1 个 xml 文件 style.xml,您在其中写了如下内容:

<style name="AppTheme" parent="Theme.Material">

然后您 运行 应用适用于 api 低于 21 的设备。 那么你有问题, api < 21 的设备不知道你的 Material 主题。

2) 您为 api>=21 使用 Material 主题,为 api < 21 使用 AppCompat,但是您的 Activity 扩展了 AppCompatActivity,那你就有问题了。因为 AppCompatActivity 需要 AppCompat 主题,而不是 material

请查看 了解更多详情 同样来自关于 AppCompatActivity:

的官方文档 page

Base class for activities that use the support library action bar features.

You can add an ActionBar to your activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme to Theme.AppCompat or a similar theme.