簡體   English   中英

Widget.MaterialComponents.MaterialCardView 未找到

[英]Widget.MaterialComponents.MaterialCardView not found

https://material.io/develop/android/theming/shape/

該站點描述了如何自定義 MaterialCardView,但是當我在樣式文件中選擇 Widget.MaterialComponents.MaterialCardView 作為我的父級時,找不到它。

我試圖用 Widget.MaterialComponents.CardView 來做,但它給我的卡片帶來了灰色背景。 我可以使用 app:cardForegroundColor 修復灰色背景,這在大多數情況下都可以正常工作,但是當我在某些特殊情況下(如 BottomSheet)使用 MaterialCardView 時,自動轉角的內置動畫會使灰色背景再次可見。 無論如何,我對 app:cardForegroundColor 不滿意,因為我認為盡可能不應該使用它。

在此處輸入圖像描述

最后,我可以通過將父級選擇為 Theme.MaterialComponents 來解決所有問題,但我仍然想知道我做錯了什么或者應該如何做。

我使用了 androidx 和 com.google.android.material:material:1.1.0

樣式:如果您使用AndroidX ,則必須有 MaterialComponents 主題作為父主題

 <style name="CardActivityTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorAccentX</item>
</style>

要以 Google 風格創建漂亮的輪廓材質卡,請在您的活動 XML 布局中創建它:

<com.google.android.material.card.MaterialCardView
            android:id="@+id/card0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="#FFF"
            app:strokeWidth="1dp"
            app:strokeColor="DADCE0"
            app:cardElevation="0dp"
            app:cardCornerRadius="8dp">
</com.google.android.material.card.MaterialCardView>

我已經對一些屬性進行了硬編碼,以便直接為您提供 colors。

結果:

卡片

如果您更喜歡普通的提升卡,而不是概述的使用此:

<com.google.android.material.card.MaterialCardView
            android:id="@+id/card0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="#FFF"
            app:cardElevation="1dp"
            app:cardCornerRadius="8dp">
</com.google.android.material.card.MaterialCardView>

請注意,我已經從前一個屬性中刪除了app:strokeWidthapp:strokeColor屬性。

最后,如果你想創建一個 animation 就像你發布的 GIF 一樣,我強烈建議使用Motion Layout。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM