android RecyclerView 背景颜色

新手上路,请多包涵

当我使用 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 许可协议

阅读 1.7k
2 个回答

我已经实现了下面的代码,这显示了卡片视图和背景颜色 在此处输入图像描述

 <?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 中删除了主要的相对布局试试这个,让我知道

//我的摇篮看起来像这样

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' }

原文由 Rakshit Nawani 发布,翻译遵循 CC BY-SA 3.0 许可协议

我也遇到了困难,直到我突然意识到,当我尝试将背景颜色设置为列表中的警告时,我需要接触 RecyclerView 的 rootView 以阻止我的应用程序崩溃。

诀窍显然是从 ListAdapter 完成这一切。例如,可以通过覆盖 RecyclerView.Adapter 中的 onAttached 方法来获取对视图的引用。

 ...
private static View mRootView;

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);

    mRootView = recyclerView.getRootView();
}

然后我创建了一种使用我在这些板上发现的技巧来更新背景颜色的方法,就像这样

public static void updateBackgroundColor() {
    if (mIsThisDbMaintenance()) {
      mRootView.getBackground().setColorFilter(Color.parseColor("#AA0000"), PorterDuff.Mode.DARKEN);
    } else {
        if (mRootView.getBackground()!=null)
            mRootView.getBackground().clearColorFilter();
    }
}

此方法是公共静态的,因此我也可以从另一个活动的菜单选项处理程序中使用它。

截屏

原文由 tbrams 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题