【问题标题】:How use two fragment in Android DrawerLayout如何在 Android DrawerLayout 中使用两个片段
【发布时间】:2013-07-23 03:10:03
【问题描述】:

我尝试使用 Android NavigationDrawe - http://developer.android.com/training/implementing-navigation/nav-drawer.html 我有一个活动和两个片段。 我的主要问题是 android.support.v4.widget.DrawerLayout 中的布局。 一个片段使用 -content_frame 和其他 - content_footer。该元素将位于底部(高度 - wrap_content)。我尝试了不同的变化,但没有奏效。所有示例都带有一个片段。我做错了什么?谢谢
我的主要布局文件。与此显示一个片段。

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

当更改为:

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

显示两个片段,但在另一个片段上。 screenshot

【问题讨论】:

    标签: java android drawerlayout


    【解决方案1】:

    如果我正确理解您的问题,您希望在顶部有一个片段,并且希望第二个片段作为屏幕上的页脚。目前还不清楚你是否想要滚动,所以我假设不是。据我所知,DrawerLayout 在布局元素方面实际上并没有做太多事情,所以我建议在 DrawerLayout 内放置一个布局,以便根据需要定位您的片段。做一些类似于我将在下面放置的未经测试的代码:

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

    祝你好运!希望对您有所帮助!

    【讨论】:

    • 当我 LinearLayout 更改为:'android:layout_width="fill_parent"' android:layout_height="fill_parent"' 和 @+id/content_frame 更改:android:layout_height="wrap_content" android:layout_weight= “1”工作并显示两个片段。工作。但是请理解,为什么不使用 RelativeLayout。
    • @egidijusb 我刚刚进行了编辑。现在它应该将 20% 的屏幕分配给页脚,将 80% 的屏幕分配给第一个片段。试试看。我的希望不是给你一个 100% 有效的例子,而是向你展示你可以在 DrawerLayout 中使用布局,从而根据需要放置东西。如果您不知道如何使用 RelativeLayout 或 LinearLayout,我无能为力。
    • @egidijusb 刚刚看到你的其他评论。它应该可以与相对布局一起使用。你一定是用错了。您必须将第一个片段设置为 alignParentTop="true" 和 alignParentLeft="true",并将其设置在页脚片段上方,并且您必须将第二个片段设置为 alignParentBottom="true" 和 alignParentLeft="true"。从那里开始,它应该只是简单的布局摆弄。
    • 工作吧。谢谢。如果我要滚动 content_frame,我必须把这个地方放到 ScrollView。是吗?
    • @egidijusb 是的,如果您希望能够滚动顶部,请将 content_frame 放入 ScrollLayout。
    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    相关资源
    最近更新 更多