android RecyclerView BackgroundColor [英] android RecyclerView BackgroundColor
本文介绍了android RecyclerView BackgroundColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用RecyclerView在我的应用中显示列表数据时,RecyclerView的背景颜色始终为白色:
When I use RecyclerView to show list data in my app, the RecyclerView's backgroundcolor always been white:
我在xml文件和代码中设置了backgroundcolor,但是它不起作用:
I set the backgroundcolor in xml file and code, but it does not work:
//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"
}
推荐答案
我实现了以下代码,它显示了卡片视图和背景色
i have implemented the below code and this is showing the card view and background color
<?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.support.v7.widget.RecyclerView
android:id="@+id/appointment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF00"
android:scrollIndicators="none" />
</RelativeLayout>
//您的适配器的布局
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
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:text="dfgjkdfh"
android:textColor="#ffffff" />
<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>
我从适配器的XML中删除了主要的相对布局.尝试并让我知道
I removed the main Relative layout from adapter's XML Try this and let me know
//我的Gradle看起来像这样
//My Gradle looks like this
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "betasoft.com.tryso"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.android.support:design:23.1.1' }
这篇关于android RecyclerView BackgroundColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文