【问题标题】:Tabhost in the action bar of Honeycomb app?Honeycomb 应用程序操作栏中的 Tabhost?
【发布时间】:2011-06-25 08:34:36
【问题描述】:

我有一个应用程序(用于 Honeycomb),其主要活动显示一种仪表板,带有三个按钮和一个标题。当用户点击一个按钮时,他们会被带到一个屏幕,在那里他们可以输入数据并进行计算。我希望在第二个(“计算器”)活动中使用两种计算方法,并希望通过在操作栏中有两个选项卡来实现这一点(仅当您在此计算器活动中时)。

我以前从未使用过一个或多个 tabhost 小部件,所以我如何在操作栏中使用一个选项卡小部件并在另一个时更改屏幕的其余部分(除了操作栏和系统栏之外的所有内容)选项卡被选中?

如果有人可以向我指出一些专门用于 Honeycomb 操作栏选项卡的源代码,那就太好了。

感谢您的帮助,祝您有美好的一天。

【问题讨论】:

    标签: android android-layout android-3.0-honeycomb android-tabhost


    【解决方案1】:

    参见Honycomb Gallery,它使用了操作栏标签。

    【讨论】:

      【解决方案2】:

      操作栏中的选项卡是一个非常简洁的功能。为了在 SO 上完成这个问题,我将提供一个示例;此代码在您的 Activity 的 onCreate 中

      final ActionBar actionBar = getActionBar();
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      
      // remove the activity title to make space for tabs
      actionBar.setDisplayShowTitleEnabled(false);
      
      
      // instantiate some fragments for the tabs
      Fragment fragment1 = new Fragment1();
      Fragment fragment2 = new Fragment2();
      
      // add a new tab and set its title text and tab listener
      actionBar.addTab(actionBar.newTab().setText(R.string.title_tab1)
                      .setTabListener(new MyTabListener(fragment1)));
      
      actionBar.addTab(actionBar.newTab().setText(R.string.title_tab2)
                      .setTabListener(new MyTabListener(fragment2)));
      

      您可以将 MyTablListener 作为您的活动的内部类,它可能看起来像这样;

      class MyTabListener implements ActionBar.TabListener {
          private Fragment fragment;
      
          public MyTabListener(Fragment fragment) {
              this.fragment = fragment;
          }
      
          public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
              ft.replace(R.id.activity_new_formula_fragment_content, fragment, null);
          }
      
          public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
              ft.remove(fragment);
          }
      
          public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-08-08
        • 1970-01-01
        • 1970-01-01
        • 2011-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多