我使用 FragmentTabHost 创建了四个带有相应 fragment 的选项卡。然而,选项卡可以工作,现在我想添加动态 UI(例如显示当前日期的 TextView 和交互式按钮),我不确定如何继续。我发现尝试将其添加到 fragment 中是不正确的,并且将其添加到主 fragment 中也会导致错误。一定有办法做到这一点,但我缺乏 Android 经验让我迷失了。那么,有没有办法给fragment添加动态UI呢?
最佳答案
我假设您已经像 the documentation 中那样创建了 FragmentTabHost .
您现在必须定义每个选项卡中的每个 fragment 。例如,在文档中,引用了 4 个 fragment :
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 实现的示例(您可以在文档中找到它们的实现)。
然后,这取决于您希望在每个选项卡中包含哪些内容(显示列表、一些按钮……)。 您可以看看培训类(class)Building a Dynamic UI with Fragments或有关 Fragment 的文档.
关于java - 如何向 fragment 选项卡添加动态 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17660906/