gpt4 book ai didi

Android "elevation"不显示阴影

转载 作者:IT老高 更新时间:2023-10-28 12:51:23 24 4
gpt4 key购买 nike

我有一个 ListView,对于每个列表项,我希望它在其下方显示一个阴影。我正在使用 Android Lollipop 的新高度功能在要转换阴影的 View 上设置 Z,并且已经使用 ActionBar(技术上是 Lollipop 中的工具栏)有效地执行此操作。我正在使用 Lollipop 的高度,但由于某种原因,它没有在列表项下显示阴影。下面是如何设置每个列表项的布局:

<?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:orientation="vertical"
style="@style/block"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/lightgray"
>

<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:elevation="30dp"
>

<ImageView
android:id="@+id/documentImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/alphared"
android:layout_alignParentBottom="true" >

<appuccino.simplyscan.Extra.CustomTextView
android:id="@+id/documentName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
app:typeface="light"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingBottom="4dp"
android:singleLine="true"
android:text="New Document"
android:textSize="27sp"/>

<appuccino.simplyscan.Extra.CustomTextView
android:id="@+id/documentPageCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
app:typeface="italic"
android:paddingLeft="16dp"
android:layout_marginBottom="16dp"
android:text="1 page"
android:textSize="20sp"/>

</LinearLayout>

</RelativeLayout>

</RelativeLayout>

但是,这里是它显示列表项的方式,没有阴影: enter image description here

我也尝试了以下方法,但无济于事:

  1. 将高度设置为 ImageView 和 TextView 本身,而不是父布局。
  2. 为 ImageView 应用背景。
  3. 使用 TranslationZ 代替 Elevation。

最佳答案

我在 Lollipop 上玩了一段时间的阴影,这就是我发现的:

  1. 似乎父 ViewGroup 的边界由于某种原因切断了其子项的阴影;和
  2. 使用 android:elevation 设置的阴影被 View 的边界截断,而不是通过边距延伸的边界;
  3. 让 subview 显示阴影的正确方法是在父 View 上设置填充并在该父 View 上设置 android:clipToPadding="false"

根据我的了解,以下是我对您的建议:

  1. 将您的顶级 RelativeLayout 设置为具有等于您在要显示阴影的相对布局上设置的边距;
  2. 在同一个RelativeLayout上设置android:clipToPadding="false"
  3. 从同样设置了高程的RelativeLayout中移除边距;
  4. [编辑]您可能还需要在需要提升的子布局上设置不透明的背景颜色。

最终,您的顶级相对布局应如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/block"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/lightgray"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"
>

内部相对布局应该是这样的:

<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="[some non-transparent color]"
android:elevation="30dp"
>

关于Android "elevation"不显示阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27477371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com