在安卓5.0以下设备上,使用CardView时出现的空白问题

6

我正在尝试为应用程序的一些视图添加圆角和阴影,并利用卡片视图库来实现。在Lollipop设备上看起来不错,但在任何早于Lollipop的设备上都会遇到兼容性问题。

首先声明,我已经查看了下面问题中的答案,发现它们都不适用于我。

最流行的答案是添加属性'cardPreventOverlap=false',但这会删除圆角。我尝试了此标志和'cardUseCompatPadding="true"'的各种变化,但似乎都没有解决问题。还有人遇到过同样的问题吗?

我的代码:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    card_view:cardCornerRadius="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="44dp"
            android:layout_height="match_parent"
            android:layout_marginRight="4dp"
            android:background="@color/mid_yellow"
            android:padding="0dp"
            android:src="@drawable/ic_add_white_24dp" />

        <TextView
            style="@style/Text.Primary.White"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:text="Button" />
    </LinearLayout>

这是在安卓5.0上的显示效果:

enter image description here

但在4.4.2版本上,同样的代码会显示为:

enter image description here

如果将 'cardPreventOverlap=false' 设置为 true,则显示效果为:

enter image description here

更新:很遗憾,我们无法解决这个问题;由于应用程序只有较少的安装基础,因此我们认为这不是很重要。最终,我们选择了第三个选项“cardPreventOverlap=false”。

2个回答

3

由于在旧设备上非常昂贵,因此不支持内容剪辑。如果您希望,可以使用Carbon。它有自己的CardView实现,可以正确地将内容剪切为圆角。Carbon还向所有其他布局添加了内容剪辑和高程,因此您可以使用带有圆角和阴影的LinearLayout来实现您的目的。请参见下图:

enter image description here


0

可以使用一个像这样的drawable来设置 cardview 的 background,而不是使用card_view:cardCornerRadius

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="4dip"/>
</shape>

你也可以为你的cardview中的每个元素都这样做,这样你就可以为每个元素指定正确的背景颜色。 在这种情况下,你只能指定某些角落:

<corners 
    android:topLeftRadius="4dp"
    android:bottomLeftRadius="4dp"/>

例如,对于您的+按钮。
cardUseCompatPadding="true"结合使用。

很抱歉,这并不会使它看起来有任何不同。如果每个元素都是一个形状,那么改变每个形状的角落并使用cardPreventCornerOverlap="false"将起作用,但是在某些需要它的地方还有图像和其他视图,我无法改变它们的角落。 - RobVoisey
但是你可以把它放在你的“linearlayout”上,对吧?使用透明的背景颜色和负填充也应该可以。 - Bas van Stein
更改线性布局的背景不会影响其子元素。方形子元素仍然会被放置在左上角,覆盖线性布局的背景;我希望子元素也是圆角的,在Lollipop上所有子元素都是正确的圆角。 - RobVoisey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接