Android抽屉布局显示在片段内容下方

4
我正在将一个抽屉布局添加到使用片段的活动中。我已按照这里所述的方式实现了此功能,即按照 Android 文档的说明进行。适配器和视图都可以正常工作,但是抽屉菜单会在片段内容下方显示。以下是所发生情况的图片:enter image description here 有人知道可能是什么原因吗?我在活动的 XML 代码和添加片段的活动代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm_list); // for drawer navigation
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    fragmentTransaction.replace(android.R.id.content,
            new AlarmListFragment());
    fragmentTransaction.commit();
    setUpDrawer(fragmentManager);
}

    private void setUpDrawer(FragmentManager fm) {
    String[] drawerItems = getResources().getStringArray(
            R.array.drawer_array);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ListView drawerList = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    drawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.item_nav_drawer, drawerItems));
    // Set the list's click listener
    drawerList.setOnItemClickListener(new
            DrawerItemClickListener(drawer,drawerList,drawerItems,fm));
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
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" >

    <ListView
        android:id="@+id/alarm_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</FrameLayout>

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

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

我需要设置z-index吗?如果需要,我可以提供更多信息。我真的很想解决这个问题!


4
你需要将该片段直接添加到主活动容器中,该容器具有id android.R.id.content,其中包含 DrawerLayout。你应该将该片段添加到侧滑窗口小部件内部的内容 FrameLayout 中。 - user
2个回答

1

我不知道是什么导致了这个问题,但要解决它,您需要将片段添加到具有ID = content_frame的FrameLayout的内容中。

所以请编辑这行代码:

fragmentTransaction.replace(android.R.id.content,
        new AlarmListFragment());

to be

 fragmentTransaction.replace(R.id.content_frame,
        new AlarmListFragment());

0

您可以在XML中将抽屉布局移动到片段容器下方。这将在应用程序的前视图中呈现DrawerLayout。只需确保不要使用抽屉的父容器覆盖片段容器,以便在需要时可以访问您的片段。

<RelativeLayout
    android:id="@+id/my_parent_container"
    android:layout_height="match_parent"
    android:layout_width="match_parent" 
    tools:context=".MainActivity"
    xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
    android:id="@+id/fragment_wrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></FrameLayout>

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

        <RelativeLayout
             android:id=@+id/drawer_pane"
             android:layout_width="250dp"
             android:layout_height="match_parent"
             android:layout_gravity="end" />
        <ListView xmlns="http://schemas.android.com/apk/res/android"
                android:layout_width="280dp"
                android:layout_height="match_parent"
                android:choiceMode="singleChoice"
                android:id="@+id/menu_list"
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

我想要把位于 DrawerLayout 内的 Toolbar 下面且位于底部导航视图上方的 Fragment 容器,该怎么做? - user19585404

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接