简体   繁体   English

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

[英]Android Drawer Layout is Displaying under fragment content

I am adding a drawer layout to my Actvity which uses fragments. 我正在为我的Actvity添加一个使用片段的抽屉布局。 I've implemented this just as described here per the android documentation. 描述我刚刚实现了这个在这里按照Android文档。 And the drawer layout Adapter and View all work in that they show up, but the drawer extends "under" the fragment content. 并且抽屉布局适配器和查看它们显示的所有工作,但抽屉在片段内容“下”延伸。 Here is a picture of what is happening: 这是一张正在发生的事情的图片: 在此输入图像描述

Does anyone know why this might be? 有谁知道为什么会这样? My code for the activity's xml, and the actiity add fragment is below: 我的活动的xml代码和actiity add片段如下:

    @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));
}

Layout: 布局:

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

Do I have to set a z-index somewhere? 我必须在某处设置z-index吗? I can provide any further info needed I'd just really like to figure this out! 我可以提供任何进一步的信息,我真的很想弄清楚这一点!

I don't what produces this issue , but to solve it u have to add the fragment to the content of your FrameLayout which has the id = content_frame . 我不会产生这个问题,但要解决它,你必须将片段添加到具有id = content_frame FrameLayout的内容中。

So edit this line 所以编辑这一行

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

to be 成为

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

You can move the drawer layout below your Fragment container in your XML. 您可以在XML中的Fragment容器下移动抽屉布局。 this will render the DrawerLayout in the forward view of the app. 这将在应用程序的前视图中呈现DrawerLayout。 just be sure not to cover the fragment container with the parent container of the drawer so that your fragment will be accessible when you need it. 请确保不要使用抽屉的父容器覆盖片段容器,以便在需要时可以访问片段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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