简体   繁体   中英

How to make elevation to project shadow on the top view, within same LinearLayout

I was able to use elevation , make top red color view ( FrameLayout ), to project shadow on bottom yellow color view ( RecyclerView ), within same LinearLayout . It looks like

在此处输入图片说明

The code is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:background="#ff0000"

        android:padding="18dp"
        android:id="@+id/button_frame_layout"
        android:elevation="18dp"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/backup_now_button"
            android:layout_marginTop="32dp"
            android:layout_marginLeft="32dp"
            android:layout_marginRight="32dp"
            android:layout_marginBottom="32dp"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center"

            android:textAllCaps="true"
            android:textSize="18sp"
            android:visibility="visible"
            android:text="BACKUP NOW" />

    </FrameLayout>

    <android.support.v7.widget.RecyclerView
        android:overScrollMode="always"
        android:fadingEdge="vertical"

        android:background="#ffff00"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"

        android:layout_weight="1"
        android:layout_height="0dp"

        android:clipToPadding="false"
        android:paddingTop="0dp"
        android:paddingBottom="16dp" />

</LinearLayout>

However, if the red view is move to bottom, the shadow is no longer projected.

在此处输入图片说明

The code is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:overScrollMode="always"
        android:fadingEdge="vertical"

        android:background="#ffff00"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"

        android:layout_weight="1"
        android:layout_height="0dp"

        android:clipToPadding="false"
        android:paddingTop="0dp"
        android:paddingBottom="16dp" />

    <FrameLayout
        android:background="#ff0000"

        android:padding="18dp"
        android:id="@+id/button_frame_layout"
        android:elevation="18dp"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/backup_now_button"
            android:layout_marginTop="32dp"
            android:layout_marginLeft="32dp"
            android:layout_marginRight="32dp"
            android:layout_marginBottom="32dp"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center"

            android:textAllCaps="true"
            android:textSize="18sp"
            android:visibility="visible"
            android:text="BACKUP NOW" />

    </FrameLayout>

</LinearLayout>

May I know why it is so? Is it possible to overcome this?

I have the check yet but you can try this,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:orientation="vertical"

android:background="#ff0000"

xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_above="@+id/button_frame_layout"
    android:elevation="18dp"
    android:background="#ffff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:overScrollMode="always"
    android:fadingEdge="vertical"

    android:id="@+id/recycler_view"
    android:layout_width="match_parent"

    android:layout_height="0dp"
    android:clipToPadding="false"
    android:paddingTop="0dp"
    android:paddingBottom="16dp" />
</RelativeLayout>
<RelativeLayout
    android:padding="18dp"
    android:id="@+id/button_frame_layout"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<Button
        android:id="@+id/backup_now_button"
        android:layout_marginTop="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="32dp"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:gravity="center"
        android:textAllCaps="true"
        android:textSize="18sp"
        android:visibility="visible"
        android:text="BACKUP NOW" />
</RelativeLayout>
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM