java - 如何向 fragment 选项卡添加动态 UI?

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

我使用 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/

相关文章:

java - 如何解决Android中的静态 fragment 需求?

java - 如何使用Java中的结构将矩形添加到jscrollpane中

python - 使用 wxpython 双缓冲

java - Eclipse RCP 案例研究

java - IntelliJ插件开发: how to generate a new source file

android - 如果文本比 View 长,动态 TextView 不会选取框

android - 如何离线安装android studio

java - 带有Java的ColorGame(如Minesweeper)

java - Spring Security 未捕获 ApplicationListener 中的登录失败

java - Android、Java 上 PEM 文件中的公钥