CardView中的Android视图立面未显示
我想在Android棒棒糖中提升CardView中的RelativeLayout,但未显示提升(阴影未显示): 我的布局文件:CardView中的Android视图立面未显示,android,android-5.0-lollipop,material-design,android-cardview,android-elevation,Android,Android 5.0 Lollipop,Material Design,Android Cardview,Android Elevation,我想在Android棒棒糖中提升CardView中的RelativeLayout,但未显示提升(阴影未显示): 我的布局文件: <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android.support.v7.cardview="htt
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.v7.cardview="http://schemas.android.com/apk/res-auto"
style="@style/CardViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
android:orientation="horizontal"
android.support.v7.cardview:cardElevation="5dp">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:paddingBottom="5dp">
<ImageView
android:id="@+id/article_thumb"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/article_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/article_thumb"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:ellipsize="middle"
android:linksClickable="false"
android:longClickable="false"
android:text="@string/title"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="@color/black" />
<TextView
android:id="@+id/article_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/article_title"
android:layout_alignStart="@+id/article_title"
android:layout_below="@+id/article_title"
android:layout_marginTop="10dp"
android:text="@string/date"
android:textAppearance="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small"
android:textColor="@color/gray" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/article_title"
android:layout_alignRight="@+id/article_title"
android:layout_below="@+id/article_title"
android:layout_marginTop="10dp"
android:elevation="30dp"
android:translationZ="30dp">
<ImageView
android:id="@+id/redbubble"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:src="@drawable/redbubble" />
<TextView
android:id="@+id/article_comment_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/white" />
</RelativeLayout>
</RelativeLayout>
有人知道为什么没有显示吗?目前,我使用Android API 21(棒棒糖)使用模拟器对其进行了测试。阴影没有显示,因为它们在RelativeLayout的边界之外绘制,并且视图被剪切到其边界
将
android:clipChildren=“false”
添加到您的RelativeLayout的父级,或者在视图中添加少量填充,并设置android:clipPadding=“false”
您应该将android:elevation属性添加到redbubble imageview中。
要查看元素的阴影,您必须向该视图添加android:elevation
我建议您将redbubble imageview更改为以下内容:
<ImageView
android:id="@+id/redbubble"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:elevation="2dp"
android:src="@drawable/redbubble" />
我注意到了同样的问题,因为行: android:layout_below=“@+id/…”
通常,这是因为阴影是在可见边界之外绘制的。在您需要的xml中的根布局中
xmlns:card_view="http://schemas.android.com/apk/res-auto"
下面是cardview的代码
card_view:cardElevation="5dp"
除去
android:hardwareAccelerated="false" from manifest
谢谢你的快速回复。如果我使用了
android:clipChildren=“false”
,那么我的整个布局就超出了范围,所以我尝试了第二种方法,并将android:padding=“5dp”
和android:clipChildren=“false”
添加到了我的RelativeLayout中。但阴影仍未显现。我做错什么了吗?你是在试图从对话泡泡形状中投射阴影吗?如果是这样,它将不起作用,因为阴影只支持凸面形状。不过,您可以尝试为圆角矩形设置轮廓提供程序。是的,我想为该气泡形状设置阴影。请尝试此链接上的解决方案。在我的情况下,它是一个透明的背景视图,带有高程。不幸的是,这不起作用。我认为alanv在主要帖子中给出的答案描述了这个问题,您可以为红泡泡添加自定义阴影,并将其设置为背景。android:background=“@drawable/myCustomShadowShape”/>虽然此代码可能会回答这个问题,但提供关于如何和/或为什么解决这个问题的额外信息将提高答案的长期价值。记住,你是在将来回答读者的问题,而不仅仅是现在提问的人!请在回答中添加解释,并说明适用的限制和假设。提到为什么这个答案比其他答案更合适也没什么坏处。
android:hardwareAccelerated="false" from manifest