问题
I'm trying to test my skills on new Google Material components.
But for now I am encountering a problem with MaterialCardView
The building process tells me
The style on this component requires your app theme to be Theme.AppCompat
[..]
at com.google.android.material.card.MaterialCardView.<init>
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.
I tried also to change my Acitivity to AppCompatActivity but without any success.
I also tried to set styles told by material.io documentation but without success !
Have you some clues?
Thanks
回答1:
As of Material Components 1.1.0-alpha06 you need to do this:
- 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 use AppCompatDelegate) - You have to use a Material Components theme
Source: https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md
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>
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.
Don't set style="@style/Theme.MaterialComponents
on the card. Don't mix themes and styles.
来源:https://stackoverflow.com/questions/50898379/materialcardview-requires-theme-appcompat