设置 Recycleviewer 背景时隐藏工具栏

Hiding Toolbar when setting Recycleviewer background

当我在 RecyclerView 上设置背景参数时,它隐藏了工具栏。

很简单,但我不知道该如何解决。

activity_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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="@android:color/darker_gray"
    tools:context="los.printers.MainActivity"
    app:theme="@style/Theme.MyTheme">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tb_main"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <RelativeLayout
        android:id="@+id/rl_fragment_container"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

fragment_printer.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="los.printers.fragment_printer">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_List"
        android:scrollbars="vertical"
        android:paddingTop="?attr/actionBarSize"
        android:background="@color/colorTextIcons"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

填充在 RecyclerView 中工作正常,但工具栏未显示。

更新activity_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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:background="@android:color/darker_gray"
        tools:context="los.printers.MainActivity"
        app:theme="@style/Theme.MyTheme">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tb_main"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <RelativeLayout
            android:id="@+id/rl_fragment_container"
             android:layout_below="@+id/tb_main"

            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>

您的 rl_fragment_container 绘制在工具栏顶部。

但我想您知道这一点,因为您在片段中应用了填充。

A padding 将以相同的大小绘制视图并应用于其内容,而边距将超出视图边界,从而使整个视图变小。如果您绘制背景,这很重要。

如前所述,您的片段位于工具栏顶部,您的回收视图填充整个片段并在顶部绘制背景。

您现在可以

  1. rl_fragment_container 移动到 xml 中的工具栏之前 -> 这会将其绘制在工具栏下方
  2. 将内边距更改为边距
  3. 或者仅使用 android:layout_below="@+id/tb_main"rl_fragment_container 布局在工具栏下方,并移除边距和边距。