【问题标题】:Android elevation of View in CardView is not shown未显示 CardView 中 View 的 Android 高程
【发布时间】:2015-02-16 06:46:43
【问题描述】:

我想在 Android Lollipop 中提升的 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="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>

所以在这个布局中,我在 RelativeLayout 中添加了这两行来制作高程:

android:elevation="30dp"
android:translationZ="30dp"

有人知道为什么它没有显示吗?目前,我使用 Android API 21 (Lollipop) 的模拟器对其进行了测试。

【问题讨论】:

  • 您是否要从该对话框气泡形状中投射阴影?如果是这样,它将不起作用,因为阴影仅支持凸形。不过,您可以尝试为圆角矩形设置轮廓提供程序。
  • 是的,我想为这个气泡形状添加阴影。
  • 在此链接上尝试解决方案stackoverflow.com/a/27900541/1972566
  • 在我的例子中,它是一个带有高度的透明视图背景

标签: android android-5.0-lollipop material-design android-cardview android-elevation


【解决方案1】:

删除

android:hardwareAccelerated="false" from manifest

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关 如何 和/或 为什么 解决问题的附加 context 将改善答案的长度长期价值。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。提及为什么这个答案比其他答案更合适也没有什么坏处。
【解决方案2】:

在你需要的xml的根布局中

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

下面是卡片视图的代码

card_view:cardElevation="5dp"

【讨论】:

    【解决方案3】:

    我注意到了同样的问题,但没有显示高程,因为行:

    android:layout_below="@+id/...."

    一般来说,这是因为阴影是在可见范围之外绘制的。

    【讨论】:

      【解决方案4】:

      您应该将 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" />
      

      【讨论】:

      • 不幸的是,这不起作用。我认为主帖中 alanv 的回答描述了问题
      • 您可以为红色气泡添加自定义阴影并将其设置为背景。 android:background="@drawable/myCustomShadowShape" />
      【解决方案5】:

      阴影未显示,因为它们被绘制在 RelativeLayout 的边界之外,并且视图被剪辑到其边界。

      将android:clipChildren="false" 添加到您的RelativeLayout 的父级或向您的视图添加少量填充并设置android:clipToPadding="false"

      【讨论】:

      • 感谢您的快速响应。如果我使用android:clipChildren="false",那么我的整个布局超出了界限,所以我尝试了第二种方法并将android:padding="5dp" 和android:clipToPadding="false" 添加到我的RelativeLayout。但是阴影仍然没有显示出来。我做错了吗?
      猜你喜欢
      • 1970-01-01
      • 2017-02-13
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多