圆角 cardView 在 RecyclerView 中不起作用 - Android?
Rounded corners cardView don't work in RecyclerView - Android?
我的 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>
我在我的 class 中写了下面的代码,但还没有工作:
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);
}
尝试在卡片视图中添加这两个属性
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
这是第一个属性的文档(其中包含对第二个属性的引用)
https://developer.android.com/reference/android/support/v7/widget/CardView.html#setPreventCornerOverlap(boolean)
这应该可以解决问题
事实证明,在 CardView
上调用 View.setBackgroundColor(int)
会删除圆角。
要更改卡片的背景颜色并保留边角,需要调用 CardView.setCardBackgroundColor(int)
。
这 post.
的某些访问者可能就是这种情况
当子类化 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 会自动将背景颜色应用到视图。这个覆盖解决了那个问题;这意味着其他开发人员不需要知道底层视图的细节。
在 xml 布局文件的最顶层布局声明中使用它:
xmlns:card_view="http://schemas.android.com/apk/res-auto"
这解决了我的问题。
我的 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>
我在我的 class 中写了下面的代码,但还没有工作:
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);
}
尝试在卡片视图中添加这两个属性
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
这是第一个属性的文档(其中包含对第二个属性的引用) https://developer.android.com/reference/android/support/v7/widget/CardView.html#setPreventCornerOverlap(boolean)
这应该可以解决问题
事实证明,在 CardView
上调用 View.setBackgroundColor(int)
会删除圆角。
要更改卡片的背景颜色并保留边角,需要调用 CardView.setCardBackgroundColor(int)
。
这 post.
的某些访问者可能就是这种情况当子类化 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 会自动将背景颜色应用到视图。这个覆盖解决了那个问题;这意味着其他开发人员不需要知道底层视图的细节。
在 xml 布局文件的最顶层布局声明中使用它:
xmlns:card_view="http://schemas.android.com/apk/res-auto"
这解决了我的问题。