安卓高度属性无法正常工作

8
自从升级到SDK 21以来,我一直在尝试使用高度属性,但似乎从未起作用。
我想要将一个框架提升到其他框架之上,所以我设置了以下内容:
android:elevation="4dp"

但是没有阴影的迹象。我尝试了按钮和帧布局,但没有得到任何高程。

所以这是完整的标签:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/panel_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:elevation="4dp"
    >

我还需要添加什么内容吗?

在IT技术方面。请注意,不要删除HTML标签。
3个回答

47
确保包含 FrameLayout 的容器的背景不是透明的。

4
没有特别指定背景的情况下,这可能导致视图中约90%的高度未能显示,因为未考虑到高度(elevation)和垂直平移(translationZ)属性。 - kandroidj
这应该是被接受的答案,因为它修复了大部分这些问题。您还应该检查https://dev59.com/bV8d5IYBdhLWcg3wpzr4#26581346 - dzielins42
1
android:elevation指定了一个视图相对于其父容器Z轴的高度,而android:translationZ则指定了视图距离其所在平面的距离。将这些属性应用于要提升并剪裁其填充的项目和其父元素上,可以帮助实现效果。 - Andrew

4
根据这里的说明,Z值等于高度值(Elevation)加上平移Z值(TranslationZ)。
因此,可以尝试以下方法:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:id="@+id/panel_card"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#ffffff"
  android:elevation="4dp"
  android:translationZ="4dp">

android:translationZ 应该仅用于动画。高度 = 海拔(静态值)+ translationZ(动画值)。 - Christophe Smet

1
您需要为其添加一个背景,类似于以下内容:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/panel_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    android:background="@drawable/dialog_background">

不要使用白色。否则阴影将无法起作用。此外,如果您不想进行动画,则不需要使用android:translationZ属性,因为它只会增加高度。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接