当我使用 RecyclerView 在我的应用程序中显示列表数据时,RecyclerView 的背景颜色始终为白色:
我在 xml 文件和代码中设置了背景颜色,但它不起作用:
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:background="@color/colorPrimary">
<!--<include layout="@layout/content_main" />-->
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
//fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:padding="5dp" />
</RelativeLayout>
//item_layout.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="@color/yellow"
card_view:cardCornerRadius="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"
android:gravity="center"
android:textColor="@color/text_white"
android:textSize="@dimen/list_item_text_size" />
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:scaleType="centerCrop" />
</RelativeLayout>
</android.support.v7.widget.CardView>
//gradle file
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.union.fmdouban"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
原文由 TonyChou 发布,翻译遵循 CC BY-SA 4.0 许可协议
我已经实现了下面的代码,这显示了卡片视图和背景颜色
//你的适配器的布局
我从适配器的 XML 中删除了主要的相对布局试试这个,让我知道
//我的摇篮看起来像这样