简体   繁体   English

Android资料设计 - LinearLayout提升

[英]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. 我只是从材料设计开始,并且除了使用CardView之外,还有一个问题需要升级才能使用。 Specifically, should it work on a LinearLayout? 具体来说,它应该在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. 如果我把它放在CardView中,高程可以正常工作,但是我在点击事件时遇到了问题。 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? 我只需要在CardView中包装我想要提升的所有内容,还是有其他方法? Thanks. 谢谢。

I am testing on a Nexus 7 running Android 5.0.2. 我正在测试运行Android 5.0.2的Nexus 7。

UPDATE 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. 看起来LinearLayout是有角度的,而不仅仅是提升。 Changing the margin doesn't seem to help. 改变保证金似乎没有帮助。 Anybody have any other ideas? 有人还有其他想法吗?

Shadows are generated for all views with ViewOutlineProvider. 使用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: 要使透明视图投射阴影,您必须设置自己的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. 默认情况下,CardView会为此添加自己的填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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