如何在Android活动中添加静态片段

时间:2014-05-02 18:24:22

标签: android fragment

尝试制作Facebook / Gmail风格Sliding Navigation Drawer。我想要的只是在XML中创建单独的片段,并在用户单击抽屉菜单中的一个列表项时显示它们。每个片段连接到列表中的一个项目。

NavigationDrawer是一个很好的示例应用程序,但它只是动态演示加载片段。我想要更简单,只需静态加载。应该如何(请使用代码片段)我在List菜单项单击中实例化我的活动中的片段? MainActivity XML将如何?

Fragment 2

Fragment 3

1 个答案:

答案 0 :(得分:1)

请阅读文档http://developer.android.com/guide/components/fragments.html

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
         android:id="+@id/fragment_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
    />
</LinearLayout>

爪哇

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
相关问题