简体   繁体   中英

How use two fragment in Android DrawerLayout

I try to use Android NavigationDrawe - http://developer.android.com/training/implementing-navigation/nav-drawer.html I have one activity and two fragments. My main problem is layout in android.support.v4.widget.DrawerLayout. One fragment use -content_frame and other - content_footer. This element would be in bottom (height - wrap_content). I have tried different variations, but did not work. All examples is with one fragment. What I am doing wrong? thank you
My main layout file. With this show one fragment.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

 <FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    >    
 </FrameLayout>

 <FrameLayout
    android:id="@+id/content_footer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    >    
 </FrameLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/abs__bright_foreground_disabled_holo_light"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" /> 
 </android.support.v4.widget.DrawerLayout>  

When change to:

     <RelativeLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         android:layout_gravity="start"
        >    
     </RelativeLayout>

     <RelativeLayout
        android:id="@+id/content_footer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        >    
     </RelativeLayout>
 </FrameLayout> 

shows both fragments, but on one another. screenshot

If I understand your problem correctly, you want to have a fragment at the top, and you want the second fragment as a footer on the screen. It's unclear if you want scrolling or not, so I'll just assume not. The DrawerLayout doesn't actually do much in terms of laying out elements to my knowledge, so I'd recommend putting a layout inside the DrawerLayout to position your fragments as you want. Do something similar to the untested code I'll put below:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
            android:layout_weight="80" >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/content_footer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20" >
    </FrameLayout>

</LinearLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/abs__bright_foreground_disabled_holo_light"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

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

Best of luck! Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM