如何将动态UI添加到片段选项卡?

时间:2013-07-15 18:09:27

标签: java android user-interface android-fragments fragment-tab-host

我使用FragmentTabHost创建了四个带有相应片段的标签。然而,标签工作,我现在想要添加动态UI(例如显示当前日期的TextView和交互式按钮)我不知道如何继续。我发现尝试将其添加到片段是不正确的,并将其添加到主要也会导致错误。必须有一些方法来做到这一点,但我缺乏Android经验让我失去了。那么,有没有办法为片段添加动态UI?

1 个答案:

答案 0 :(得分:1)

我假设您已经在the documentation中创建了FragmentTabHost。

您现在要定义每个标签中的每个片段。例如,在doc中,引用了4个片段:

    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
            FragmentStackSupport.CountingFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
            LoaderCursorSupport.CursorLoaderListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
            LoaderCustomSupport.AppListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
            LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
  • FragmentStackSupport.CountingFragment
  • LoaderCursorSupport.CursorLoaderListFragment
  • LoaderCustomSupport.AppListFragment
  • LoaderThrottleSupport.ThrottledLoaderListFragment

这些只是Fragment实现的示例(您可以在doc中找到它们的实现)。

然后,它取决于您在每个标签中的内容(显示列表,几个按钮,......)。 您可以查看培训课程Building a Dynamic UI with Fragments或有关Fragment的文档。