【问题标题】:Rounded corners cardView don't work in RecyclerView - Android?圆角 cardView 在 RecyclerView - Android 中不起作用?
【发布时间】:2016-09-05 17:21:05
【问题描述】:

我的安卓设备是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>

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

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);
}

【问题讨论】:

标签: android android-recyclerview android-cardview


【解决方案1】:

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

要更改卡片的背景颜色并保留边角,需要调用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);
}

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

【讨论】:

  • 这行得通,我将该方法应用于我的代码并按预期工作。
  • 很容易犯这个错误!
  • 谢谢,伙计!在拉我的头发将近 30 分钟后发现了这个。
【解决方案2】:

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

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

这解决了我的问题。

【讨论】:

    【解决方案3】:

    尝试在卡片视图中添加这两个属性

    card_view:cardPreventCornerOverlap="false"
    card_view:cardUseCompatPadding="true"
    

    这是第一个属性的文档(其中包含对第二个属性的引用) https://developer.android.com/reference/android/support/v7/widget/CardView.html#setPreventCornerOverlap(boolean)

    这应该可以解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-16
      • 2021-05-04
      • 2020-11-14
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多