Android:带有两个片段的内容视图的 DrawerLayout

Android: DrawerLayout with content view with two fragments

我正在为平板电脑创建布局,我有一个 DrawerLayout,它在左侧菜单(即抽屉)上有一个 Fragment,应该有两个片段作为主要内容。

我的做法如下:

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

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

    <View
        android:id="@+id/right_card_group_divider"
        android:layout_width="1dip"
        android:layout_height="match_parent"
        android:background="@drawable/grey_line_bg"/>

    <FrameLayout
        android:id="@+id/menu_frame_two"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.3"/>
</LinearLayout>

<!-- The navigation drawer -->
<FrameLayout
    android:id="@+id/menu_frame"
    android:layout_width="300dp"
    android:layout_gravity="start"
    android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>

然而,我注意到如果我手动隐藏 "menu_frame_two",DrawerLayout 可以完美地工作,但是如果 Fragment 没有被隐藏,那么当打开 DrawerLayout 屏幕上什么也没有出现:它变暗了,就像打开了抽屉一样。

左抽屉菜单没有显示的原因是什么?

您的 menu_frame_two FrameLayout 似乎在 menu_frame 导航抽屉上方。所以那个时候导航抽屉是不可见的

解决方案:

  • 使用单个 FrameLayout 并在其中添加 main content screen 的所有元素
 <FrameLayout android:id = "@+id/content_frame"         
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
   <FrameLayout
    android:id="@+id/content_frame_inside"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3"/>

<View
    android:id="@+id/right_card_group_divider"
    android:layout_width="1dip"
    android:layout_height="match_parent"
    android:background="@drawable/grey_line_bg"/>

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

原因:

  • 根据 Documentation 应该有一个 content_frame 的高度和宽度等于 match_parent