首页 > 解决方案 > Androidx 迁移后 MaterialCardView 不会受其样式影响

问题描述

我正在开发新的 android 应用程序,该应用程序使用谷歌材料设计组件显示咖啡饮料。在我将项目迁移到 Androidx 后,样式将不适用于 MaterialCardView IDK 有什么问题或为什么会发生这种情况!!。

我尝试使用最新的材料库版本升级我的 gradle 文件,并在网上搜索找不到任何类似的问题。

 <!--this is the card layout:-->

    <com.google.android.material.card.MaterialCardView  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coffee_card"
    style="@style/CoffeeItemStyle"
    android:layout_width="180dp"
    android:layout_height="200dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="@dimen/cardview_margin"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="@dimen/cardview_margin">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView
            android:id="@+id/coffee_image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:contentDescription="@string/coffee_drink_image"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/coffee_name"
            style="@style/CoffeeNameStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:text="Name"
            app:layout_constraintBottom_toTopOf="@+id/coffee_image"
            app:layout_constraintStart_toStartOf="@+id/coffee_image"
            app:layout_constraintTop_toBottomOf="@+id/coffee_image"
            app:layout_constraintVertical_bias="0.82" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.card.MaterialCardView>

<!--this is the style(CoffeeItemStyle):-->

     <style name="CoffeeItemStyle"     parent="Widget.MaterialComponents.CardView">
        <item name="cardBackgroundColor">@android:color/white</item>
        <item name="cardCornerRadius">@dimen/cardview_radius</item>
        <item name="cardElevation">@dimen/cardview_elevation</item>
        <item name="android:clickable">true</item>
        <item name="android:stateListAnimator">
            @animator/shr_next_button_state_list_anim
        </item>

    </style>

我希望卡片视图出现在 4dp 半径、8dp 高度、白色背景并在点击卡片视图时更改 Z 轴。

下图显示了迁移后的样子 在此处输入图像描述

标签: javaxmlandroid-studiomaterial-designandroidx

解决方案


推荐阅读