简体   繁体   English

带固定标签的操作栏

[英]Action Bar with Fixed Tabs

Right now in my application I have a TabActivity with three fixed tabs each of them containing a standard Activity . 现在,在我的应用程序中,我有一个带有三个固定选项卡的TabActivity ,每个选项卡都包含一个标准的Activity

Here is the relative code snippet: 以下是相关代码段:

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);
}

In this way each time I select a Tab the application will show on screen the Activity associated to that that tab. 这样,每次我选择一个选项卡时,应用程序将在屏幕上显示与该选项卡关联的Activity

I'd like to use the ActionBar design pattern to obtain the same result. 我想使用ActionBar设计模式来获得相同的结果。

For now I'm just interested in having the fixed tab layout as you can see here : 现在我只想对固定的标签布局感兴趣,你可以在这里看到:

在此输入图像描述

How should I change my code? 我该如何更改代码?

These SO questions did not help much: 这些SO问题没有多大帮助:

In this way each time I select a Tab the application will show on screen the Activity associated to that that tab. 这样,每次我选择一个选项卡时,应用程序将在屏幕上显示与该选项卡关联的活动。

This approach is officially deprecated. 这种方法已被正式弃用。

I'd like to use the ActionBar design pattern to obtain the same result. 我想使用ActionBar设计模式来获得相同的结果。 How should I change my code? 我该如何更改代码?

The most straightforward approach is to convert those nested activities to fragments, then commit FragmentTransaction s as the selected tab changes. 最直接的方法是将这些嵌套活动转换为片段,然后在选定的选项卡更改时提交FragmentTransaction If you are using the native API Level 11 action bar, you can use the native API Level 11 Fragment class and the FragmentTransaction passed into your TabListener . 如果您使用的是本机API Level 11操作栏,则可以使用本机API Level 11 Fragment类和传递给TabListenerFragmentTransaction If you wish to use ActionBarSherlock, you will wind up using the Fragment and FragmentTransaction from the Android Support package. 如果您希望使用ActionBarSherlock,您将最终使用Android支持包中的FragmentFragmentTransaction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM