Android 的CardView不显示阴影

tech2022-12-08  106

    今天在项目中遇到一个不大不小的问题。

    1、为什么是不大的问题呢,因为确实是一个很小的问题,就是给一个布局的边框简单的增加阴影。

    2、为什么又是一个不小的问题呢,因为我刚开始的想法是通过layer-list写一个xml文件,代码一顿敲后,结果完全达不到效果,真可谓是一顿操作猛如虎,伤害只有0.5。

    3、坚信办法总比困难多。试试把布局嵌套在CardView中吧,结果无论怎么配置都不显示阴影,哎,傻眼了,上火啊,人生啊,茫茫啊!

    4、嗯,好吧,不得不承认,几乎没用过CardView,对它的各个属性还不熟悉,静下心来,慢慢学习一下:

cardBackgroundColor背景颜色cardCornerRadius圆角大小cardElevationz轴的阴影大小cardMaxElevationz轴的阴影最大高度值cardUseCompatPadding是否使用CompatPaddingcardPreventCornerOverlap是否使用PreventCornerOverlapcontentPadding内容于边距的间隔contentPaddingLeft内容与左边的边距contentPaddingTop内容与顶部的边距contentPaddingRight内容与右边的边距contentPaddingBottom内容与底部的边距

    cardUseCompatPadding:在Android 5.0及以下的系统中,CardView会添加一个额外的padding来绘制阴影,但是在Android 5.0以上的系统中是没有这个padding的,是直接绘制阴影。因此在Android 5.0以上系统,设置为false时,CardView不会专门去添加padding来绘制阴影;设置为true时会添加padding后绘制阴影,才与Android 5.0以下的效果保持一致。我们来看看该属性的源代码配置情况:

mCompatPadding = a.getBoolean(R.styleable.CardView_cardUseCompatPadding, false);

    由此可见,cardUseCompatPadding属性的默认值为false,即不添加padding,因此我们就看不到阴影。

    cardPreventCornerOverlap:该属性表示是否添加内边距,用来避免内容与边缘重叠。设置为true时,也就是添加padding,使得content不与圆角重叠;设置为false时,也就是不添加padding,content将与圆角重叠,圆角被覆盖。我们也来看看该属性的源代码配置情况:

mPreventCornerOverlap = a.getBoolean(R.styleable.CardView_cardPreventCornerOverlap, true);

    由此可见,cardPreventCornerOverlap属性的默认值为true,即添加padding,使得content不与圆角重叠。我们顺便看看其他属性的源代码配置情况吧:

if (a.hasValue(R.styleable.CardView_cardBackgroundColor)) { backgroundColor = a.getColorStateList(R.styleable.CardView_cardBackgroundColor); } else { // There isn't one set, so we'll compute one based on the theme final TypedArray aa = getContext().obtainStyledAttributes(COLOR_BACKGROUND_ATTR); final int themeColorBackground = aa.getColor(0, 0); aa.recycle(); // If the theme colorBackground is light, use our own light color, otherwise dark final float[] hsv = new float[3]; Color.colorToHSV(themeColorBackground, hsv); backgroundColor = ColorStateList.valueOf(hsv[2] > 0.5f ? getResources().getColor(R.color.cardview_light_background) : getResources().getColor(R.color.cardview_dark_background)); } float radius = a.getDimension(R.styleable.CardView_cardCornerRadius, 0); float elevation = a.getDimension(R.styleable.CardView_cardElevation, 0); float maxElevation = a.getDimension(R.styleable.CardView_cardMaxElevation, 0); int defaultPadding = a.getDimensionPixelSize(R.styleable.CardView_contentPadding, 0); mContentPadding.left = a.getDimensionPixelSize(R.styleable.CardView_contentPaddingLeft, defaultPadding); mContentPadding.top = a.getDimensionPixelSize(R.styleable.CardView_contentPaddingTop, defaultPadding); mContentPadding.right = a.getDimensionPixelSize(R.styleable.CardView_contentPaddingRight, defaultPadding); mContentPadding.bottom = a.getDimensionPixelSize(R.styleable.CardView_contentPaddingBottom, defaultPadding); if (elevation > maxElevation) { maxElevation = elevation; } mUserSetMinWidth = a.getDimensionPixelSize(R.styleable.CardView_android_minWidth, 0); mUserSetMinHeight = a.getDimensionPixelSize(R.styleable.CardView_android_minHeight, 0); a.recycle();

    在理解了CardView的属性含义以及默认配置后就好办了,只是做如下简单的配置就能够看到阴影了:

<androidx.cardview.widget.CardView android:layout_width="wrap_content" android:layout_height="wrap_content" app:cardBackgroundColor="@color/primary_bg_gray" app:cardCornerRadius="@dimen/card_corners" app:cardUseCompatPadding="true"> <!-- 这里自由填充布局控件 --> </androidx.cardview.widget.CardView>

    有的人说,还要在配置文件中开启硬件加速:android:hardwareAccelerated="true",个人觉得随意吧。

------记录工作中的点点滴滴,书写码农的平凡岁月!------

最新回复(0)