【问题标题】:Action Bar with Fixed Tabs带有固定选项卡的操作栏
【发布时间】:2012-08-07 11:17:51
【问题描述】:

现在在我的应用程序中,我有一个 TabActivity,其中包含三个固定选项卡,每个选项卡都包含一个标准 Activity

这里是相关代码sn-p:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tab_activity);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Tab1Activity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("tab1").setIndicator("Tab 1", res.getDrawable(R.drawable.Tab1Icon)).setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, Tab2Activity.class);
    spec = tabHost.newTabSpec("tab2").setIndicator("Tab 2", res.getDrawable(R.drawable.Tab2Icon)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Tab3Activity.class);
    spec = tabHost.newTabSpec("tab3").setIndicator("Tab 3", res.getDrawable(R.drawable.Tab3Icon)).setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

这样,每次我选择一个选项卡时,应用程序都会在屏幕上显示与该选项卡关联的Activity

我想使用ActionBar 设计模式来获得相同的结果。

现在我只对固定选项卡布局感兴趣,如您所见here

我应该如何更改我的代码?

这些 SO 问题没有多大帮助:

【问题讨论】:

  • 您是否至少使用 API 14?
  • @Se_bastiaan - 在我目前的版本中没有,但我想写一个支持 API 14 的新版本。

标签: java android android-layout android-tabhost android-actionbar


【解决方案1】:

这样,每次我选择一个选项卡时,应用程序都会在屏幕上显示与该选项卡关联的活动。

此方法已正式弃用。

我想使用 ActionBar 设计模式来获得相同的结果。我应该如何更改我的代码?

最直接的方法是将那些嵌套的活动转换为片段,然后在选定的选项卡更改时提交FragmentTransactions。如果您使用的是本机 API 级别 11 操作栏,则可以使用本机 API 级别 11 Fragment 类和传递给您的 TabListenerFragmentTransaction。如果您想使用 ActionBarSherlock,您将使用 Android 支持包中的 FragmentFragmentTransaction

【讨论】:

  • 能否请您发布一些代码 sn-ps 以便更好地了解我应该编写的代码的结构?提前谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多