1

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

这里是我的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>

谢谢您的时间和h ELP!

回答

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

谢谢!它正在工作。为什么我的执行不起作用? –

+0

这是布局,把视图垂直或horitzonzal你应该使用LinearLayout,或者你可以使用RelativeLayout设置layout_below等。在你的情况下,可能LinearLayout不适用于AppBarLayout和DrawerLayout。请接受答案,谢谢! – MarcGV

+0

有道理。我一定忘记了基本的东西。我的错! –

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

感谢您的回答。我使用Relativelayout修复了这个问题。 –

+0

在我看来,'LinearLayout'在这种情况下更直接,但我很高兴你能解决这个问题。 –

+0

然后,我将不得不删除'android:layout_marginTop =“?attr/actionBarSize”'。我还有其他要添加的组件。所以,我需要外部的相对布局。 –