android - 更改 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>
解决方案
这是最终奏效的方法
<?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>
推荐阅读
- themes - 我们能否在下一次年度调查中加入深色主题与浅色主题的调查
- c# - 如何从 SQL Server Compact C# 在实体框架中更快地读取数据
- json - 我想从 spring 中获取一个 javascript 数组以列出
- angular - 如何防止Router将不同的路由视为路由参数?
- mysql - 连接 sql 行
- python - 如何删除列中的索引
- angular - 为什么我全新的 Angular8 应用程序显示一个空页面和空的应用程序根
- c++ - image.rows 和 image.cols 小于真实图像大小(c++)
- python - 如果请求因代码失败,请重试类方法
- c# - 如何将此方法转换为异步