我无法用setElevation绘制阴影。我不想使用图形来执行此操作。这是代码:
这是父母。
Page.xml
setElevation不显示阴影
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/background">
<LinearLayout
android:id="@+id/pp_cont_new_list"
android:background="@color/background"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="5dp">
</LinearLayout>
</LinearLayout>
我在这里建立一个简单的布局。
PersonalPage.Java
final LinearLayout ROOT = (LinearLayout) findViewById(R.Id.pp_cont_new_list);
final LinearLayout LL = new LinearLayout(context); //Container
LL.setLayoutParams (new LinearLayout.LayoutParams(
Generic.PARAM_MP, //eq. match_parent
(int) context.getResources().getDimension (R.dimen.bt_preview_list_height)
));
LL.setBackgroundResource (R.drawable.background_new_list);
LL.setElevation (5f);
LL.setClickable (true);
ROOT.addView(LL);
/*
* other code inner the container
*/
此背景
background_new_list.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="@color/background"/>
</shape>
</item>
<item>
<ripple android:color="@color/textColorAlpha"/>
</item>
</layer-list>
我想我的错误的解释。
我也想知道如何使用LOLLIPOP之前的版本。
棒棒糖及以上的解决方案。获取子视图以显示阴影的正确方法是在父级上设置填充,并在该父级上设置“android:clipToPadding =”false“”。这是解释。 对于wick.ed:https://stackoverflow.com/a/27518160/886148 –
:当你看到填充那里,但即使我添加你的解决方案不起作用 – Thorny84
你有没有添加填充到父级布局?你能告诉我更新的代码吗?此外,'颜色/背景' –