Android资料设计 – LinearLayout提升

我只是从材料设计开始,并且除了使用CardView之外,还有一个问题需要升级才能使用.具体来说,它应该在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>

此代码不会产生可见的布局高程 – 没有阴影.如果我把它放在CardView中,高程可以正常工作,但是我在点击事件时遇到了问题.我试过删除图像,但这没有效果.我只需要在CardView中包装我想要提升的所有内容,还是有其他方法?谢谢.

我正在测试运行Android 5.0.2的Nexus 7.

UPDATE

我按照建议尝试了大纲提供程序,这会产生阴影,但却是一个奇怪的阴影.

看起来LinearLayout是有角度的,而不仅仅是提升.改变保证金似乎没有帮助.有人还有其他想法吗?

最佳答案 使用ViewOutlineProvider为所有视图生成阴影.如果设置了背景,则会从视图的背景中自动生成此类提供程序.阴影采用背景的形状和透明度.要使透明视图投射阴影,您必须设置自己的ViewOutlineProvider:

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

确保阴影施法者有足够的空间来绘制阴影.默认情况下,CardView会为此添加自己的填充.

点赞