MaterialCardview需要Theme.AppCompat [英] MaterialCardview requires Theme.AppCompat
问题描述
我正在尝试测试我在新的Google Material组件上的技能.
但是现在我遇到MaterialCardView
I'm trying to test my skills on new Google Material components.
But for now I am encountering a problem with MaterialCardView
构建过程告诉我
The style on this component requires your app theme to be Theme.AppCompat
[..]
at com.google.android.material.card.MaterialCardView.<init>
通过这个线索,我添加了
style="@style/Theme.AppCompat"
& android:theme="@style/Theme.AppCompat"
到MaterialCardView
以及清单中的我的活动".
With this clue I added
style="@style/Theme.AppCompat"
& android:theme="@style/Theme.AppCompat"
to the MaterialCardView
and also to my Activity in the manifest.
我也尝试将Acitivity更改为AppCompatActivity,但没有成功.
I tried also to change my Acitivity to AppCompatActivity but without any success.
我还尝试设置material.io文档中介绍的样式,但没有成功!
I also tried to set styles told by material.io documentation but without success !
您有一些线索吗?
谢谢
推荐答案
从材料组件1.1.0-alpha06开始,您需要执行以下操作:
As of Material Components 1.1.0-alpha06 you need to do this:
- 取决于库
implementation 'com.google.android.material:material:1.1.0-alpha06'
- 您需要拥有
compileSdkVersion 28
- 您的活动需要扩展
AppCompatActivity
(或使用AppCompatDelegate
) - 您必须使用材料组件"主题
- Depend on the library
implementation 'com.google.android.material:material:1.1.0-alpha06'
- You'll need to have
compileSdkVersion 28
- Your activity needs to extend
AppCompatActivity
(or useAppCompatDelegate
) - You have to use a Material Components theme
最简单的入门方法是将当前主题扩展为Theme.MaterialComponents.*.Bridge
而不是Theme.AppCompat.*
.
The easiest way to get started is for your current theme to extend Theme.MaterialComponents.*.Bridge
instead of Theme.AppCompat.*
.
此外,您需要在主题中覆盖以下属性,否则卡的颜色将为损坏:
Additionally you'll need to override the following attribute in your theme, otherwise the card color will be broken:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<item name="elevationOverlaysEnabled">false</item>
</style>
请勿在卡上设置android:theme="@style/Theme.MaterialComponents"
.卡中每个小部件上的主题都会丢失颜色信息(主要,次要,重点...).
Don't set android:theme="@style/Theme.MaterialComponents"
on the card. You'll lose color information (primary, secondary, accent,...) from your theme on every widget inside the card.
请勿在卡上设置style="@style/Theme.MaterialComponents
. 不要混合主题和样式.
这篇关于MaterialCardview需要Theme.AppCompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!