圆角cardView在RecyclerView中不起作用 - Android?

9 android android-cardview android-recyclerview

我的Android设备是4.3和不在角落的工作cardView:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/CardStart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:scaleType="centerCrop"
    app:cardUseCompatPadding="true"
    card_view:cardBackgroundColor="@color/BlackTrans"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txtDescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/imgbIcon"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/ic_serch" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

我在课堂上写了下面的代码,但还没有工作:

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
{
    holder.CardStart.setCardElevation(0);
    holder.CardStart.setBackgroundColor(ContextCompat.getColor(context,R.color.BlackTrans));
    holder.CardStart.setRadius(5);
    holder.CardStart.setUseCompatPadding(true);
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ier 47

事实证明,调用View.setBackgroundColor(int)a CardView会删除圆角.

要更改卡片的背景颜色并保留角落,需要拨打电话CardView.setCardBackgroundColor(int).

对于这篇文章的某些访问者可能就是这种情况.


在进行子类化时CardView,我建议添加以下方法来保护您的角落免遭意外删除:

/**
 * Override prevents {@link View#setBackgroundColor(int)} being called,
 * which removes the rounded corners.
 */
@Override
public void setBackgroundColor(@ColorInt int backgroundColor) {
    setCardBackgroundColor(backgroundColor);
}
Run Code Online (Sandbox Code Playgroud)

特别是,我正在为React Native开发一个自定义视图实现,并且React自动将背景颜色应用于视图.这个覆盖解决了这个问题; 这意味着其他开发人员不需要知道底层视图的细节.

  • 这项工作,我将方法应用于我的代码并按预期工作。 (2认同)
  • 很容易犯这个错误! (2认同)

小智 6

在 xml 布局文件的最顶层布局声明中使用它:

xmlns:card_view="http://schemas.android.com/apk/res-auto"

这为我解决了这个问题。