简体   繁体   中英

Android material design - LinearLayout elevation

I'm just starting with material design and having a problem getting elevation to work with anything but a CardView. Specifically, should it work on a LinearLayout?

<LinearLayout
            android:paddingTop="10dp"
            android:orientation="vertical"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="2dp">
            <LinearLayout
                android:id="@+id/shareLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="10dp">

                <ImageView
                    android:id="@+id/shareIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_share_black_48dp"/>
                <TextView
                    android:id="@+id/shareText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:paddingLeft="10dp"
                    android:text="@string/action_share"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_computer_black_48dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:paddingLeft="10dp"
                    android:text="@string/action_desktop"/>
            </LinearLayout>

        </LinearLayout>

This code produces no visible elevation of the layout - no shadow. If I put it in a CardView, the elevation works, but then I have problems getting click events to fire. I have tried removing the images, but that has no effect. Do I just have to wrap everything I want elevated in a CardView, or is there another way? Thanks.

I am testing on a Nexus 7 running Android 5.0.2.

UPDATE

I tried the outline provider as suggested, and that results in a shadow, but a strange one.

截图

It looks like the LinearLayout is angled or something instead of just elevated. Changing the margin doesn't seem to help. Anybody have any other ideas?

Shadows are generated for all views with ViewOutlineProvider. Such provider is generated automatically from view's background if a background is set. The shadow takes the shape and the transparency of the background. To make a transparent view cast a shadow, you have to set your own ViewOutlineProvider:

view.setOutlineProvider(new ViewOutlineProvider() {
    @Override
    public void getOutline(View view, Outline outline) {
        outline.setRect(0, 0, view.getWidth(), view.getHeight());
    }
});

Make sure that the shadow caster has enough space to draw its shadow. CardView adds its own padding for that purpose by default.

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