阴影在布局中不起作用 Android

Shadows doesn't work in layout Android

我有阴影问题。在 Android Studio 预览中显示:

Editor preview

但在模拟器和 phone 中我看到:

Emulator preview

有我的xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".Profil"
android:background="@color/lightback"
android:orientation="vertical">


<FrameLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:id="@+id/testFragmentu2"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"

    android:translationZ="10dp"
    android:background="@android:color/transparent"></FrameLayout>

<FrameLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="175dp"
    android:id="@+id/testFragmentu"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:translationZ="0dp"

    android:background="@android:color/transparent"
    android:layout_marginTop="10dp"></FrameLayout>

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="150dp"

    android:translationZ="10dp"
    android:background="@android:color/transparent"
    android:layout_marginTop="10dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right|top"
        android:background="@android:color/transparent">

        <TextView android:id="@id/android:empty"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:text="Niestety, wszyscy mają partnerów na siłownię... :(" />


        <ListView android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:drawSelectorOnTop="false" />


    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

我正在尝试创建 3 个片段。当我点击第三个片段的项目时,其中两个应该从顶部出现。但是没有影子。我设置了 translationZ 但它不起作用...

对于阴影,你应该像这样使用 elevation :

app:elevation="10dp"

而不是translationZ

如android docs中所述,elevation是“视图的基本z深度”,这是一个静态变量,而translationZ是动态的。

elevation

base z depth of the view.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp".

您应该使用 translationZ 制作动画。