我有一个CustomView工作在前Lollipop,现在我尝试在Lollipop设备上应用
android:elevation和android:translateZ但似乎不起作用.
<com.example.CustomView android:id="@+id/myview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:elevation="10dp"> </com.example.CustomView>
我错过了什么?
解决方法
如
Defining Shadows and Clipping Views所述
您应该实现ViewOutlineProvider
抽象类,View构建其Outline
,用于阴影投射和剪切
矩形CustomView
public class CustomView extends View { // .. @Override protected void onSizeChanged(int w,int h,int oldw,int oldh) { /// .. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setOutlineProvider(new CustomOutline(w,h)); } } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private class CustomOutline extends ViewOutlineProvider { int width; int height; CustomOutline(int width,int height) { this.width = width; this.height = height; } @Override public void getOutline(View view,Outline outline) { outline.setRect(0,width,height); } } //... }