[英]CardView not showing shadow elevation
I am working with cardviews but the problem is my CardView is not showing any elevation or shadow.我正在使用 cardviews,但问题是我的 CardView 没有显示任何高程或阴影。 I have already tried some of the methods suggested in stackoverflow answers like I have tried using elevation and shadow property.
我已经尝试过 stackoverflow 答案中建议的一些方法,就像我尝试过使用 elevation 和 shadow 属性一样。 I have also tried using card_view:cardUseCompatPadding="true", but without any success.
我也尝试过使用 card_view:cardUseCompatPadding="true",但没有成功。 This is my xml file
这是我的 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>
Dont know why it is not showing any elevation and shadow不知道为什么它没有显示任何高度和阴影
Do not forget that to draw shadow you must use hardwareAccelerated drawing不要忘记,要绘制阴影,您必须使用 hardwareAccelerated drawing
<application android:hardwareAccelerated="true" ...>
see for details https://developer.android.com/guide/topics/graphics/hardware-accel.html?hl=ru有关详细信息,请参阅https://developer.android.com/guide/topics/graphics/hardware-accel.html?hl=ru
<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"
is the main catch here app:cardUseCompatPadding="true"
是这里的主要问题
If your manifest file has a android:hardwareAccelerated="false"
line, delete it.如果您的清单文件包含
android:hardwareAccelerated="false"
行,请将其删除。
Lot of good answers, but sometimes just one doesn't work for all.很多好的答案,但有时只有一个并不适用于所有人。 here's the one that worked for me after a lot of looking around,
经过大量环顾四周,这是对我有用的那个,
Add this line to your CardView
将此行添加到您的
CardView
app:cardUseCompatPadding="true"
So your cardview looks like this所以你的卡片视图看起来像这样
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
app:cardUseCompatPadding="true"/>
Hope that works!希望有用! Happy coding!
编码愉快!
use this inside cardView to show shadow:在 cardView 中使用它来显示阴影:
app:cardElevation="4dp"
app:cardMaxElevation="4dp"
You should have to try this:你应该试试这个:
<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>
I am working with cardviews but the problem is my CardView is not showing any elevation or shadow.我正在使用 cardviews,但问题是我的 CardView 没有显示任何高度或阴影。 I have already tried some of the methods suggested in stackoverflow answers like I have tried using elevation and shadow property.
我已经尝试过一些在 stackoverflow 答案中建议的方法,就像我尝试过使用高程和阴影属性一样。 I have also tried using card_view:cardUseCompatPadding="true", but without any success.
我也尝试过使用 card_view:cardUseCompatPadding="true",但没有成功。 This is my xml file
这是我的 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>
Dont know why it is not showing any elevation and shadow不知道为什么它没有显示任何高度和阴影
You forget two things.你忘记了两件事。
AndroidManifest.xml
file, in the Application
tag you have to writeAndroidManifest.xml
文件,在你必须写的Application
标签中<application android:hardwareAccelerated="true"...>
instead of代替
<application android:hardwareAccelerated="false"...>
Then check the parent tag of your layout file is there written like below line.然后检查布局文件的父标签是否像下面这样写。
if you wanna use app:
xmlns.如果你想使用
app:
xmlns。 then write: xmlns:app="http://schemas.android.com/apk/res-auto"
然后写:
xmlns:app="http://schemas.android.com/apk/res-auto"
OR xmlns:card_view="http://schemas.android.com/apk/res-auto"
或者
xmlns:card_view="http://schemas.android.com/apk/res-auto"
NOTE: You don't need to write both simultaneous.注意:您不需要同时写入两者。 Because it gives you duplicate attribute error.
因为它会给你重复的属性错误。
The above line is most important when you use the attributes of 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>
Try this试试这个
<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"/>
And make sure that you are using this dependency in gradle并确保您在 gradle 中使用此依赖项
compile 'com.android.support:cardview-v7:23.2.1'
remove below line from AndroidManifest.xml
从
AndroidManifest.xml
中删除以下行
android:hardwareAccelerated="false"
There are two things that helped me solve this problem:有两件事帮助我解决了这个问题:
Setting this in the application tag of my manifest:在我的清单的应用程序标签中设置它:
android:hardwareAccelerated="false"
File > Invalidate cache and restart文件 > 使缓存无效并重新启动
Add this line in CardView widget在 CardView 小部件中添加此行
app:cardElevation="5dp"
hope this will help!希望这会有所帮助!
<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>
Use above code and don't forget to use android:hardwareAccelerated="true"
.使用上面的代码,不要忘记使用
android:hardwareAccelerated="true"
。 It will work definitely.它肯定会起作用。
If there is a parent for the Card View , try setting a background color for the parent layout like a color #EDEDED, anything other than white.如果Card View 有父级,请尝试为父级布局设置背景颜色,例如 #EDEDED 颜色,除白色以外的任何颜色。 This will also make a clear difference between card and the background.
这也将在卡片和背景之间产生明显的区别。
<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" on the parent layout makes the difference.父布局上的 android:background="#EDEDED"有所不同。
And don't use android:elevation="10dp"
, use app:cardElevation="10dp"
并且不要使用
android:elevation="10dp"
,使用app:cardElevation="10dp"
Try putting this in your rootview:尝试将其放入您的根视图中:
xmlns:card_view="http://schemas.android.com/apk/res-auto"
and use this code for elevation:并使用此代码进行提升:
card_view:cardElevation="10dp"
<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>
Remove the background color first, add the elevation and later add the background again.首先删除背景颜色,添加高程,然后再次添加背景。 Worked for me, though I had added the background through the layout designer.
为我工作,虽然我已经通过布局设计器添加了背景。
Apparently, I was using Theme.AppCompat instead of Theme.MaterialComponents in as my AppTheme.显然,我使用 Theme.AppCompat 而不是 Theme.MaterialComponents 作为我的 AppTheme。
<style name="Theme.Main" parent="Theme.MaterialComponents.NoActionBar">
</style>
In your layout, use MaterialCardView instead of CardView在您的布局中,使用 MaterialCardView 而不是 CardView
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
Try adding android:outlineProvider="bounds"
to the CardView layout.尝试将
android:outlineProvider="bounds"
添加到 CardView 布局。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.