首页 > 解决方案 > CardView 显示白色居中布局

问题描述

我在回收站视图中使用卡片视图作为行项目,每个卡片视图都有不同的背景颜色。在我的卡片视图中添加圆角半径之前,我已经成功完成了。现在它在中间显示一些白色布局。

这是我的 cardView 的代码。

    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    android:id="@+id/cvNB"
    app:cardCornerRadius="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/txtNBHead"
                android:layout_weight="0.9"
                android:textSize="24sp"
                android:fontFamily="@font/raleway_semibold"
                android:textColor="@color/colorPrimary"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:id="@+id/NBOptions"
                android:src="@drawable/ic_menu_icon_white_24dp"
                android:layout_weight="0.1"/>
        </LinearLayout>
        <TextView
            android:id="@+id/txtNBStory1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/raleway_thin"
            android:textSize="18sp"
            android:textColor="@color/colorPrimary"
            android:layout_margin="2dp"/>
        <TextView
            android:id="@+id/txtNBStory2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/raleway_thin"
            android:textSize="18sp"
            android:textColor="@color/colorPrimary"
            android:layout_margin="2dp"/>
        <TextView
            android:id="@+id/txtNBStory3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/raleway_thin"
            android:textSize="18sp"
            android:textColor="@color/colorPrimary"
            android:layout_margin="2dp"/>
    </LinearLayout>
</androidx.cardview.widget.CardView>

我在回收站视图中使用它,下面的代码显示了不同的背景颜色

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
        holder.cvNB.getBackground().setAlpha(0);
    } else {
        holder.cvNB.setCardBackgroundColor(ContextCompat.getColor(context, colors[index]));
    }

这是我得到的 Cardview 显示白色部分

我真的很沮丧,请帮帮我

这是回收站视图布局的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/listMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:scrollbars="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"/>
    </ScrollView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:src="@drawable/icon_add"
        app:backgroundTint="@color/colorProfileText"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.airbnb.lottie.LottieAnimationView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listNBAnim"
    app:lottie_fileName="nb_loading_anim.json"
    app:lottie_loop="true"
    android:visibility="gone"
    app:lottie_autoPlay="true"/>
</RelativeLayout>

标签: javaandroidxmllayoutandroid-cardview

解决方案


推荐阅读