首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓-嵌套DrawerLayout?或者是否有实现导航抽屉的替代方法?

安卓-嵌套DrawerLayout?或者是否有实现导航抽屉的替代方法?
EN

Stack Overflow用户
提问于 2014-05-22 04:33:57
回答 1查看 1.3K关注 0票数 2

我想用如下的导航抽屉为一个活动做一个布局:

代码语言:javascript
复制
Navigation drawer closed:
-------------------------
|                       |
|                       |
|                       |
|                   1   |
|                       |
|                       |
|                       |
|                       |
|                       |
|                       |
|                       |
|-----------------------|
|        2              |
-------------------------


Navigation drawer open:
-------------------------
|                |      |
|                |      |
|     3          |      |
|                |  1   |
|                |      |
|                |      |
|                |      |
|                |      |
|                |      |
|                |      |
|                |      |
|-----------------------|
|        2              |
-------------------------

第1节和第3节应该在同一个DrawerLayout中(在main_section中)。第2节和main_section应该在活动的同一层次上。

然而,DrawerLayout的文档指出,'DrawerLayout充当窗口内容的顶级容器,允许从窗口边缘提取交互式“抽屉”视图。因此,我想我计划用DrawerLayout实现什么是不可能的,因为我需要DrawerLayout作为父布局。

我应该怎么做才能实现上面的布局结构?

更新,也许我需要添加更多的需求细节。第2节不应受到导航抽屉状态的视觉或功能影响。因此,这意味着第2节不应该像第1节所做的那样,当抽屉打开时,触摸第2节不应该关闭抽屉。无论抽屉是否打开,第2节应始终主动响应用户交互。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-24 09:36:34

好的,我知道了。

下面是我的代码解决方案的片段:

fragment_navigation_drawer.xml:

代码语言:javascript
复制
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/navigation_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccc"
    android:orientation="vertical"
    tools:context=".NavigationDrawerFragment">

    <!-- add the content of the Section 3 (the navigation drawer) here.
     this entire xml file could be a listview 
     instead of a linear layout, btw. -->

</LinearLayout>

fragment_main.xml:

代码语言:javascript
复制
<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"
    android:background="#ccc"
    tools:context=".MainActivity$PlaceholderFragment">

    <!-- add the content of the Section 1 here. -->

</RelativeLayout>

activity_main.xml:

代码语言:javascript
复制
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7"
        tools:context=".MainActivity">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <fragment android:id="@+id/navigation_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:name=".NavigationDrawerFragment"
            tools:layout="@layout/fragment_navigation_drawer" />
    </android.support.v4.widget.DrawerLayout>


    <!-- This button below itself is the Section 3. -->
    <Button
        android:text="@string/hello_world"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#F66"
        android:onClick="test"/>

</LinearLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23797873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档