【问题标题】:Card View in Android not shownig shadow and cornersAndroid中的卡片视图不显示阴影和角落
【发布时间】:2021-04-01 09:20:31
【问题描述】:

CardView 中的阴影不起作用。当我在卡片视图中放置任何东西时,卡片视图中的角落也消失了。在 Android Studio 中的 Preview 工具看起来非常好,但对于我所有的设备来说看起来并不相似。 我在 Android 9.0 上工作。这个版本有问题吗?

我的材料设计版本 实现(“com.google.android.material:material:1.3.0”)

Preview in Android Studio tools

What showing on my tablet screen

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="130dp"
    android:layout_height="180dp"
    android:layout_margin="10dp"
    android:background="@android:color/transparent">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="5dp"

        app:cardElevation="4dp"
        app:cardPreventCornerOverlap="true"
        app:cardUseCompatPadding="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <RelativeLayout
                android:id="@+id/relative_image_view"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.75"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <ImageView
                    android:id="@+id/itemImage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:background="@drawable/emaenu_game"
                    android:contentDescription="@string/picture"
                    android:scaleType="centerCrop" />

            </RelativeLayout>

            <View
                android:id="@+id/view10"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="@color/main_emenu"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.25"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/relative_image_view" />

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:fontFamily="@font/poppins"
                android:gravity="center"
                android:minHeight="0dp"
                android:paddingStart="15dp"
                android:paddingTop="2dp"
                android:paddingEnd="15dp"
                android:text="Water Flow"
                android:textAllCaps="false"
                android:textColor="@color/black_emenu"
                android:textSize="12sp"
                android:textStyle="normal"
                app:layout_constraintBottom_toBottomOf="@+id/view10"
                app:layout_constraintEnd_toEndOf="@+id/view10"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/relative_image_view" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-layout kotlin android-cardview


    【解决方案1】:

    请将此添加到cardView

    app:cardElevation="3dp"
    app:cardCornerRadius="16dp"
    

    请删除这 2 行

    app:cardPreventCornerOverlap="true"
    app:cardUseCompatPadding="true"
    

    并且 CardView 将是 Parent 布局而不是 ConstraintLayout 所以请移除上面的 ConstraintLayout。

    <com.google.android.material.card.MaterialCardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
        
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/AnyColor">
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
    

    我确信在此之后它会显示阴影和圆角半径。

    【讨论】:

    • 它工作但不在我的项目中。可能是主题或类似的问题吗?
    • @PawełKrzyściak 原因是您使用了一些属性阻止了材料卡视图的主要属性。你已经删除了上约束布局?如果是,则删除所有背景颜色并仅放置角半径和 cardElevation。和高度和宽度,然后它必须工作。
    • 谢谢,我找到了适合我的解决方案.. 我忘记在清单 android:hardwareAccelerated="false" 中删除这一行
    猜你喜欢
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    相关资源
    最近更新 更多