首页 > 解决方案 > 更改 Recyclerview 背景颜色会使工具栏消失 - 为什么会这样

问题描述

我正在尝试更改我的 recyclerview 背景颜色,它在 android oreo 和 android Q 中运行良好,我试图在 kitkat 和 jellybean 中测试我的应用程序,但是发生了一些奇怪的事情。当我使用这条线时android:background="@color/backgroundColorRecyclerView",我的标题栏消失了,并且发生了类似的事情标题栏不见了

当我删除这条线时,每件事都像这样正常工作在此处输入图像描述

我一直试图弄清楚几个小时没有任何效果。

这是我的 app_bar.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我的 content_main .xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:paddingTop="60dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroundColorRecyclerView"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/admob_banner_ad_main">
    </com.google.android.gms.ads.AdView>


</RelativeLayout>

标签: androidandroid-actionbarandroid-toolbarandroid-recyclerview

解决方案


这是最终奏效的方法

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroundColorRecyclerView"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/admob_banner_ad_main">
    </com.google.android.gms.ads.AdView>


</RelativeLayout>

推荐阅读