简体   繁体   English

CardView 里面的 CardView 没有阴影

[英]CardView inside CardView Doesn't Have Shadow

I have a CardView inside another CardView , but the child CardView doesnt have shadow around it.另一个CardView有一个CardView ,但是CardView周围没有阴影。 Any idea why?知道为什么吗?

        <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
            card_view:cardBackgroundColor="@color/WHITE_COLOR"
            card_view:cardCornerRadius="20dp"
            card_view:cardElevation="3dp"
            card_view:cardPreventCornerOverlap="false"
            card_view:contentPadding="0dp">

            <android.support.v7.widget.CardView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_centerInParent="true"
                card_view:cardBackgroundColor="@color/RED_COLOR"
                card_view:cardCornerRadius="20dp"
                card_view:cardElevation="6dp"
                card_view:cardPreventCornerOverlap="false"
                card_view:contentPadding="0dp">


            </android.support.v7.widget.CardView>

        </android.support.v7.widget.CardView>

I have met exactly the same problem我遇到了完全相同的问题

card_view:cardUseCompatPadding="true" 

solved it解决了

so every card looks like所以每张卡片看起来像

                <android.support.v7.widget.CardView
                    card_view:cardUseCompatPadding="true"
                    card_view:cardElevation="4dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                   ...
                </android.support.v7.widget.CardView>

Use this code temporarily..暂时使用此代码..

<android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                card_view:cardBackgroundColor="@color/WHITE_COLOR"
                card_view:cardCornerRadius="20dp"
                card_view:cardElevation="3dp"
                card_view:cardPreventCornerOverlap="false"
                card_view:contentPadding="0dp">

                <android.support.v7.widget.CardView
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:layout_centerInParent="true"
                    card_view:cardBackgroundColor="@color/RED_COLOR"
                    card_view:cardCornerRadius="20dp"
                    card_view:cardElevation="6dp"
                    card_view:cardPreventCornerOverlap="false"
                    card_view:contentPadding="0dp">


                </android.support.v7.widget.CardView>
                 <View
                android:layout_width="match_parent"
                android:layout_height="4dp"
                android:background="@drawable/shadow" />

            </android.support.v7.widget.CardView>

Include this xml code in drawable folder under the name shadow.xml将此 xml 代码包含在名称为 shadow.xml 的 drawable 文件夹中

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="@android:color/transparent"
        android:endColor="#40000000"
        android:angle="90" />
</shape

You can add View below CardView having property layout_marginTop .你可以有属性layout_marginTop下面添加CardView查看 My problem is solve by doing this, hope your's too.我的问题是通过这样做来解决的,希望你的也是。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"

    <androidx.cardview.widget.CardView
        android:id="@+id/cvLogin"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="20dp"
        app:cardElevation="2dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="false">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_margin="1dp"
            app:cardBackgroundColor="@color/colorPrimary"
            app:cardCornerRadius="20dp"
            app:cardElevation="0dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Log In"
                    android:textAllCaps="true"
                    android:textColor="@android:color/white"
                    android:textSize="13sp" />

        </androidx.cardview.widget.CardView>

    </androidx.cardview.widget.CardView>

    <View
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_marginTop="5dp" />
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM