我只是从材料设计开始,除了使用卡片视图外,在获取立面图方面遇到了问题。具体来说,它应该在线性布局上工作吗?
<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中,这个提升就起作用了,但是我很难让click事件触发。我试过删除这些图像,但没有效果。我只需要把我想要的东西都放在卡片上,还是有别的办法?谢谢。
我正在运行android 5.0.2的nexus 7上进行测试。
更新
我按建议尝试了大纲提供程序,结果出现了阴影,但很奇怪。
看起来线性布局是有角度的,而不是升高的。改变保证金似乎没有帮助。有人有其他想法吗?
最佳答案
使用ViewOutlineProvider为所有视图生成阴影。如果设置了背景,则会从视图的背景自动生成此类提供程序。阴影的形状和背景的透明度。要使透明视图投射阴影,必须设置自己的ViewOutlineProvider:
view.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRect(0, 0, view.getWidth(), view.getHeight());
}
});
确保阴影投射器有足够的空间绘制阴影。默认情况下,CardView会为此添加自己的填充。