简体   繁体   English

CardView cardUseCompatPadding

[英]CardView cardUseCompatPadding

I am developing an Android app for both Lollipop and Previous versions. 我正在为Lollipop和早期版本开发Android应用程序。

I am using CardView (This cardView does not have any child, it simply placed behind my View) to create shadow. 我正在使用CardView(这个cardView没有任何孩子,它只是放在我的View后面)来创建阴影。

But the problem arise when it runs on the pre Lollipop devices. 但是当它在前Lollipop设备上运行时会出现问题。

so I set cardUseCompatPadding to true. 所以我将cardUseCompatPadding设置为true。 I am wondering if I could get the value of this compat padding? 我想知道我是否可以获得这个compat填充的值?

Is there anywhere I could find the reference to the value? 有没有我能找到价值的参考?

The compat padding added to the CardView depends on the elevation and the radius of the corners you have set. 添加到CardView的compat填充取决于您设置的角的高度和半径。 You can find the actual calculation in the RoundRectDrawableWithShadow class in the support library. 您可以在支持库中的RoundRectDrawableWithShadow类中找到实际计算。

You can calculate it at runtime using something like: 您可以在运行时使用以下内容计算它:

    float elevation = cardView.getMaxCardElevation();
    float radius = cardView.getRadius();
    double cos45 = Math.cos(Math.toRadians(45));

    int horizontalPadding = (int) (elevation + (1 - cos45) * radius);
    int verticalPadding = (int) (elevation * 1.5 + (1 - cos45) * radius);

Updates the backward compatible maximum elevation of the CardView. 更新CardView的向后兼容的最大高程。

Calling this method has no effect if device OS version is Lollipop or newer and getUseCompatPadding() is false. 如果设备操作系统版本为Lollipop或更新且getUseCompatPadding()为false,则调用此方法无效。 Use this code 使用此代码

android:elevation="3dp" or app:elevation="3dp" to your cardview android:elevation="3dp"app:elevation="3dp"到你的cardview

CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms. CardView使用Lollipop上的提升属性来显示阴影,然后回退到旧平台上的自定义模拟阴影实现。

Due to expensive nature of rounded corner clipping, on platforms before Lollipop, CardView does not clip its children that intersect with rounded corners. 由于圆角切割的昂贵性质,在Lollipop之前的平台上,CardView不会剪切与圆角相交的孩子。 Instead, it adds padding to avoid such intersection (See setPreventCornerOverlap(boolean) to change this behavior). 相反,它添加了填充以避免这种交集(请参阅setPreventCornerOverlap(boolean)来更改此行为)。

Before Lollipop, CardView adds padding to its content and draws shadows to that area. 在Lollipop之前,CardView会在其内容中添加填充并为该区域绘制阴影。 This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom. 此填充量等于两侧的maxCardElevation +(1 - cos45)* cornerRadius,顶部和底部的maxCardElevation * 1.5 +(1 - cos45)* cornerRadius。

Since padding is used to offset content for shadows, you cannot set padding on CardView. 由于填充用于偏移阴影的内容,因此无法在CardView上设置填充。 Instead, you can use content padding attributes in XML or setContentPadding(int, int, int, int) in code to set the padding between the edges of the CardView and children of CardView. 相反,您可以在代码中使用XML中的内容填充属性或代码中的setContentPadding(int,int,int,int)来设置CardView边缘和CardView子节点之间的填充。

Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before Lollipop and after Lollipop. 请注意,如果您为CardView指定精确尺寸,由于阴影,其内容区域在Lollipop之前和Lollipop之后的平台之间会有所不同。 By using api version specific resource values, you can avoid these changes. 通过使用api版本特定的资源值,您可以避免这些更改。 Alternatively, If you want CardView to add inner padding on platforms Lollipop and after as well, you can call setUseCompatPadding(boolean) and pass true. 或者,如果您希望CardView在Lollipop平台上添加内部填充,之后也可以调用setUseCompatPadding(boolean)并传递true。

To change CardView's elevation in a backward compatible way, use setCardElevation(float). 要以向后兼容的方式更改CardView的高程,请使用setCardElevation(float)。 CardView will use elevation API on Lollipop and before Lollipop, it will change the shadow size. CardView将在Lollipop上使用提升API,在Lollipop之前,它将改变阴影大小。 To avoid moving the View while shadow size is changing, shadow size is clamped by getMaxCardElevation(). 为避免在阴影大小发生变化时移动视图,请使用getMaxCardElevation()来限制阴影大小。 If you want to change elevation dynamically, you should call setMaxCardElevation(float) when CardView is initialized. 如果要动态更改高程,则应在初始化CardView时调用setMaxCardElevation(float)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM