0

正如标题所说,我试图在我的应用程序中设置这个 TabView,每次我去运行那个活动时,只是一个黑屏?我查看了一些教程,但仍然没有运气,这就是我的活动。

protected void onCreate(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabView);
    TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
    tabHost.setup();

    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
    getActionBar().setDisplayShowTitleEnabled(false);

    TabSpec tabSpec1 = tabHost.newTabSpec("tag1");
    tabSpec1.setContent(R.id.Tab1);
    tabSpec1.setIndicator("Tab1");
    tabHost.addTab(tabSpec1); 

    TabSpec tabSpec2 = tabHost.newTabSpec("tag2");
    tabSpec2.setContent(R.id.Tab2);
    tabSpec2.setIndicator("Tab2");
    tabHost.addTab(tabSpec2);

    TabSpec tabSpec3 = tabHost.newTabSpec("tag3");
    tabSpec3.setContent(R.id.Tab3);
    tabSpec3.setIndicator("Tab3");
    tabHost.addTab(tabSpec3);
}
4

2 回答 2

1

Not sure if you are adding Tabs to the activity screen or the actionBar. But if it's the former, something like this will help:

public class MainActivity extends TabActivity implements TabHost.TabContentFactory {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("tab1", getResources().getDrawable(R.drawable.stat_happy))
                .setContent(this));
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("tab2", getResources().getDrawable(R.drawable.stat_neutral))
                .setContent(this));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("tab3", getResources().getDrawable(R.drawable.stat_sad))
                .setContent(this));

    }

    @Override
    public View createTabContent(String tag) {
        final TextView tv = new TextView(this);
        tv.setText("Content for tab with tag " + tag);
        return tv;
    }
}
于 2013-10-15T21:51:47.223 回答
0

To add tabs in action bar, you need to set the navigation mode first bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); to your action bar and then add tabs.

Check this sample tutorial.

于 2013-10-15T21:40:03.197 回答