【问题标题】:Android Drawer Layout is Displaying under fragment contentAndroid Drawer Layout 在片段内容下显示
【发布时间】:2014-03-29 15:41:58
【问题描述】:

我正在向使用片段的 Activity 添加一个抽屉布局。我已经按照 android 文档中描述的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 吗?我可以提供任何需要的进一步信息,我真的很想弄清楚这一点!

【问题讨论】:

  • 您将片段直接添加到主活动容器中,该容器的 ID 为 android.R.id.content,其中包含 DrawerLayout。您应该将片段添加到内容FrameLayout inside 抽屉小部件。

标签: java android android-layout listview android-fragments


【解决方案1】:

我不知道是什么产生了这个问题,但要解决它,你必须将片段添加到你的 FrameLayout 的内容中,它的 ID = content_frame

所以编辑这一行

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

成为

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

【讨论】:

    【解决方案2】:

    您可以在 XML 中将抽屉布局移动到 Fragment 容器下方。这将在应用程序的前视图中呈现 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多