CardView 不显示阴影高程

CardView not showing shadow elevation

我正在使用卡片视图,但问题是我的卡片视图没有显示任何高度或阴影。我已经尝试了一些在 Whosebug 答案中建议的方法,比如我尝试使用高程和阴影 属性。我也尝试过使用 card_view:cardUseCompatPadding="true",但没有成功。这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    app:cardBackgroundColor="#FAFBFD"
    app:cardElevation="5dp">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/post_profile_image"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/ic_launcher"
                android:layout_marginLeft="12dp"
                android:layout_marginTop="12dp"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/post_profile_image"
                android:layout_marginLeft="12dp"
                android:orientation="vertical"
                android:layout_marginTop="16dp">

                <TextView
                    android:id="@+id/post_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#000000"/>

                <TextView
                    android:id="@+id/post_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="Time"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/grayColor"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true">

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:drawable/ic_delete"
                    android:id="@+id/post_delete_btn"
                    android:visibility="invisible"/>

            </LinearLayout>

        </RelativeLayout>

        <ImageView
            android:id="@+id/post_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="12dp"
            android:adjustViewBounds="true"
            android:background="#00ffffff"
            android:src="@drawable/add_btn"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/post_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="12dp"
            android:text="Text about Posting...."
            android:textAppearance="?android:attr/textAppearanceMedium"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="8dp">

            <ImageButton
                android:id="@+id/like_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12dp"
                android:background="#00ffffff"
                android:src="@drawable/grey_like_bt"/>

            <TextView
                android:id="@+id/post_like_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginTop="8dp"
                android:text="0 Likes"
                android:textColor="#2196F3"/>

            <ImageButton
                android:id="@+id/comment_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="32dp"
                android:background="#00ffffff"
                android:src="@drawable/comment_btn_gray"/>

            <TextView
                android:id="@+id/post_comment_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginTop="8dp"
                android:text="0 Comments"
                android:textColor="#2196F3"/>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:drawable/ic_menu_edit"
                    android:visibility="invisible"
                    android:id="@+id/post_edit_btn"
                    android:layout_alignParentRight="true"/>

            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>

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

不知道为什么没有显示任何高度和阴影

你应该试试这个:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardElevation="3dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>

        <!--Put your other fields between this-->
</android.support.v7.widget.CardView>

在 cardView 中使用这个来显示阴影:

app:cardElevation="4dp"
app:cardMaxElevation="4dp"

尝试将其放入您的根视图中:

xmlns:card_view="http://schemas.android.com/apk/res-auto" 

并使用此代码进行提升:

card_view:cardElevation="10dp"
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@android:color/white"
    android:foreground="?android:attr/selectableItemBackground"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/padding_small"
    android:layout_marginBottom="@dimen/padding_small"
    app:cardCornerRadius="4dp"
    app:cardElevation="4dp" >

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

在 CardView 小部件中添加此行

app:cardElevation="5dp"

希望这会有所帮助!

<android.support.v7.widget.CardView android:id="@+id/item_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="2dp"
    app:cardUseCompatPadding="true"
    app:cardElevation="2dp"
    app:cardBackgroundColor="@color/md_white_1000"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true">

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

app:cardUseCompatPadding="true" 是这里的主要收获

试试这个

    <android.support.v7.widget.CardView
            android:id="@+id/driverCurrentJobsCardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/dim_8"
            android:background="#ffffff"
            app:cardCornerRadius="4dp"
            card_view:cardElevation="4dp"/>

并确保您在 gradle

中使用此依赖项
compile 'com.android.support:cardview-v7:23.2.1'

不要忘记绘制阴影必须使用硬件加速绘图

<application android:hardwareAccelerated="true" ...>

查看详情 https://developer.android.com/guide/topics/graphics/hardware-accel.html?hl=ru

     <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardCornerRadius="4dp"
    app:cardBackgroundColor="#ffff8800">
     ......
     </android.support.v7.widget.CardView>

先去除背景颜色,添加高程,然后再添加背景。为我工作,虽然我已经通过布局设计器添加了背景。

如果您的清单文件有 android:hardwareAccelerated="false" 行,请将其删除。

AndroidManifest.xml

中删除下一行
android:hardwareAccelerated="false"
<android.support.design.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    app:cardCornerRadius="@dimen/_5sdp"
    card_view:cardCornerRadius="3dp"
    android:layout_margin="@dimen/_3sdp"
    card_view:cardElevation="10dp"
    card_view:cardUseCompatPadding="true">
</android.support.design.card.MaterialCardView>

使用上面的代码,不要忘记使用 android:hardwareAccelerated="true"。 它肯定会起作用。

你忘记了两件事。

  • 首先,检查你项目的AndroidManifest.xml文件,在Application标签中你必须写

<application android:hardwareAccelerated="true" ...>

而不是

<application android:hardwareAccelerated="false" ...>

  1. 然后检查你的布局文件的父标签是否像下面这样写。

  2. 如果你想使用 app: xmlns。然后写 : xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:card_view="http://schemas.android.com/apk/res-auto"

注意:您不需要同时写入。因为它会给你重复的属性错误。

当你使用 res-auto 的属性时,上面一行是最重要的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp" />

</RelativeLayout>

尝试将 android:outlineProvider="bounds" 添加到 CardView 布局。

很多好的答案,但有时只有一个答案并不适用于所有人。 这是经过大量环顾后对我有用的那个,

将此行添加到您的 CardView

app:cardUseCompatPadding="true"

所以你的卡片视图看起来像这样

  <androidx.cardview.widget.CardView
      android:id="@+id/cardView"
      android:layout_width="match_parent"
      app:cardUseCompatPadding="true"/>

希望有用!编码愉快!

有两件事帮助我解决了这个问题:

  1. 在我的清单的应用程序标签中设置:

    android:hardwareAccelerated="false"
    
  2. 文件 > 使缓存无效并重新启动

如果卡片视图有父级尝试为父级布局设置背景颜色,如颜色#EDEDED,除了白色以外的任何东西。这样也会让卡片和背景有明显的区别。

<androidx.constraintlayout.widget.ConstraintLayout 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_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EDEDED"
    >
 <com.google.android.material.card.MaterialCardView
        android:id="@+id/cardone"        
        android:elevation="10dp"   
        android:layout_margin="10dp"
       >

android:background="#EDEDED" 在父布局上 有所不同。

不要用 android:elevation="10dp",用app:cardElevation="10dp"

显然,我使用 Theme.AppCompat 而不是 Theme.MaterialComponents 作为我的 AppTheme。

 <style name="Theme.Main" parent="Theme.MaterialComponents.NoActionBar">
</style>

在您的布局中,使用 MaterialCardView 而不是 CardView

<com.google.android.material.card.MaterialCardView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    app:cardElevation="10dp">