Cardview没有显示底部阴影

39

我有以下用于卡片视图的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="2dp">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="0dp">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">
        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这导致了下面完全没有阴影的这些阴影.

http://i.imgur.com/xNoIDR4.png

但我想要得到像这个阴影一样更漂亮地环绕整张卡片。

enter image description here

我尝试过改变高度,但无济于事。搜寻此问题时似乎只出现了毫无阴影问题的人,而这不是我的情况。

如何让阴影正确呢?

谢谢。


3
可能是在Android 5.0上设置AppCompat CardView中的XML高度的重复问题。 - MLProgrammer-CiM
2
谢谢,这解决了我的问题。不过有点奇怪,为什么在API v21及其之后需要添加这个标签呢?我理解得对吗? - Zerp
1
这只是Android的碎片化问题,只需要在Stack Overflow上搜索并继续进行下一个功能即可。 - MLProgrammer-CiM
我能否只在底部显示阴影(卡片视图的一侧)? - Ibrahim Disouki
https://dev59.com/FF4c5IYBdhLWcg3wsb_I#39123951 - deviant
5个回答

123

您需要在卡片视图中添加以下XML属性:

app:cardUseCompatPadding="true"

该属性可以提高应用程序的兼容性和适配性。

在顶部,类似于:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" ... - Junior Mayhé

5
在父级 LinearLayout 中添加 clipChildren="false" 也可以达到同样的效果。

2
使用card_view: cardElevation属性设置高程。

30
根据我的问题描述,我已经尝试过这个方法,但并不是因为这个问题。在阅读了 MLProgrammer-CiM 的评论后,问题解决了。原来我需要使用 card_view:cardUseCompatPadding="true" - Zerp

1
直到我想起来,如果父级元素包裹其内容,则不会包裹阴影,所以我给父级元素分配了比子元素更大的尺寸,这样它就会包含子元素和子元素的阴影,然后问题解决了。
 <androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="110dp"
android:layout_height="110dp">

<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_width="98dp"
    android:layout_height="98dp"
    app:cardCornerRadius="10dp"
    app:cardElevation="5dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.appcompat.widget.AppCompatImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/manhattan" />

  
</androidx.cardview.widget.CardView>

  </androidx.constraintlayout.widget.ConstraintLayout>

0

在你的cardView中添加这些行

    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"

请考虑在您的答案中添加一些解释。 - Corné

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接