android RecyclerView BackgroundColor

爷,独闯天下 提交于 2021-01-28 03:13:00

问题


When I use RecyclerView to show list data in my app, the RecyclerView's backgroundcolor always been white:

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

回答1:


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>

//Your adapter's layout

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

I removed the main Relative layout from adapter's XML Try this and let me know

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



回答2:


You can also set background color with condition in RecyclerView. Just in your RecyclerViewAdapter, add following line in onBindViewHolder method:

mRecyclerViewHolder.mRecyclerViewRowLayout.setBackgroundColor(condition.isRight() ? 0xFFAED581 : 0xFFE57373);

In case if you wondering what's arguments in SetBackgroundColor, here is it:

condition is just normal Boolean condition.

0xFFAED581 is just hex color code with 0xFF prefix(Programmatically, we need to use color code with 0xFF prefix instead of # ).




回答3:


I was having a hard time with this as well, until it dawned on me that I needed to get my hands on the rootView of the RecyclerView to stop my app from crashing when I tried setting a background color as a warning on my list.

The trick was apparently to do it all from the ListAdapter. Getting hold of a reference to the view can for example be done by overwriting the onAttached method in the RecyclerView.Adapter.

...
private static View mRootView;

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

    mRootView = recyclerView.getRootView();
}

Then I created a method for updating the background color using a trick I found on these boards like this

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

This method is public static, so that I could use it from the menu option handler in another activity as well.

screenshot



来源:https://stackoverflow.com/questions/35980486/android-recyclerview-backgroundcolor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!