2017-06-19 95 views
0

我无法用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之前的版本。

+0

棒棒糖及以上的解决方案。获取子视图以显示阴影的正确方法是在父级上设置填充,并在该父级上设置“android:clipToPadding =”false“”。这是解释。 对于wick.ed:https://stackoverflow.com/a/27518160/886148 –

+0

:当你看到填充那里,但即使我添加你的解决方案不起作用 – Thorny84

+0

你有没有添加填充到父级布局?你能告诉我更新的代码吗?此外,'颜色/背景' –

回答

1

集抬高只是不列入预棒棒糖

工作,你可以创建一个有阴影的自定义按钮图像,并将其设置为按钮background..or任何视图的背景给阴影效果

+0

谢谢,但我的代码在LOLLIPOP之前和LOLLIPOP之后不工作 – Thorny84

0

使用ViewCompat.setElevation(your_view(in your case LL), elevation_amount); 但对于设备上预棒棒糖这不会有效果, 预棒棒糖情况下,你可以使用:

android:background="@android:drawable/dialog_holo_light_frame" 
+0

我以前尝试过,但它不起作用 – Thorny84

0

这是工作,如果你不介意做的事情在XM湖我试过了。我没有使用背景颜色值,所以我使用了不同的颜色作为父级布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#E8E8E8" 
android:paddingBottom="20dp" 
android:clipToPadding="false" 
android:translationZ="6dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:background="@drawable/background_new_list" 
    android:clipToPadding="false" 
    android:elevation="25dp" 
    android:orientation="vertical" 
    /> 

注:我使用的是更高的高程值只是为了更容易地检查,如果它的工作。你可以根据你的需要进行调整。