gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:03 25 4
gpt4 key购买 nike

我的 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>

我在类里面写了下面的代码,但还没有工作:

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

最佳答案

事实证明,在 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 开发自定义 View 实现,React 会自动将背景颜色应用到 View 。这个覆盖解决了那个问题;这意味着其他开发人员不需要知道底层 View 的详细信息。

关于android - 圆角 cardView 在 RecyclerView 中不起作用 - Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39335214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com