在Appbar下方设置Recyclerview

时间:2017-03-08 11:14:33

标签: android android-layout android-recyclerview android-relativelayout

我想在activity_main中设置一个appbar。出于某种原因,我无法正确设置它。它被设置在回收者视图下方。

这是我的activity_main

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />




    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation = "vertical"
        android:layout_marginTop="?attr/actionBarSize"
        tools:context=".MainActivity">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_heritage_sites"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"/>

    </RelativeLayout>


</android.support.v4.widget.DrawerLayout>

这是我的app_bar_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorWhite"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>




</android.support.design.widget.CoordinatorLayout>

感谢您的时间和帮助!

2 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

         <include
             android:id="@+id/appbar"
             layout="@layout/app_bar_main"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" />

         <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_below="@id/appbar"
            tools:context=".MainActivity">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview_heritage_sites"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical"/>

        </RelativeLayout>
    </RelativeLayout>


</android.support.v4.widget.DrawerLayout>

答案 1 :(得分:1)

您可以使用LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation = "vertical"
            android:layout_marginTop="?attr/actionBarSize"
            tools:context=".MainActivity">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview_heritage_sites"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical"/>

        </RelativeLayout>

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>