0

我正在努力让一个简单的 tabhost 与 ABS 一起工作,似乎 Android 在尝试“改进”它们时只会使事情变得更加复杂。我正在考虑将我的项目转换为在标签中使用片段,但事实证明,对于我在每个标签中使用的复杂布局来说,这是一种方式。我现在只需要一个普通的 ol 工作 tabhost w。工作腹肌吧。

package com.abs.tabs.fragments;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;

import com.actionbarsherlock.view.MenuItem;
import com.buhz.feeds.Buhdz;
import com.buhz.login.R;
import com.buhzhyve.localz.Localz;
import com.buhzhyve.trails.Trails;
import com.buhzhyve.trendz.Trendz;

public class FragmentTabs extends SherlockActivity {
    ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         setTheme(SampleList.THEME); //Used for theme switching in samples
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);


        actionBar=getSupportActionBar();

         Resources res = getResources(); // Resource object to get Drawables

            TabHost tabHost =(TabHost) findViewById(android.R.id.tabhost);  // The activity TabHost

            TabHost.TabSpec spec;  // Resusable TabSpec for each tab

            Intent intent;  // Reusable Intent for each tab

            // Do the same for the other tabs

            intent = new Intent().setClass(this, Buhdz.class);

            spec = tabHost.newTabSpec("Buhdz").setIndicator("Feeds",

                              res.getDrawable(R.drawable.buhdz_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);

         // Do the same for the other tabs

            intent = new Intent().setClass(this, Localz.class);

            spec = tabHost.newTabSpec("Localz").setIndicator("Locals",

                              res.getDrawable(R.drawable.locals_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);



            intent = new Intent().setClass(this, Trendz.class);

            spec = tabHost.newTabSpec("Trendz").setIndicator("Trends",

                              res.getDrawable(R.drawable.trends_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);



            intent = new Intent().setClass(this, Localz.class);

            spec = tabHost.newTabSpec("convos").setIndicator("Convos",

                              res.getDrawable(R.drawable.convos_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);

         // Create an Intent to launch an Activity for the tab (to be reused)

            intent = new Intent().setClass(this, Trails.class);

            // Initialize a TabSpec for each tab and add it to the TabHost

            spec = tabHost.newTabSpec("Trails").setIndicator("Trails",

                              res.getDrawable(R.drawable.trails_tab_icon))

                          .setContent(intent);

            tabHost.addTab(spec);
            tabHost.setCurrentTab(4);
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.item1:
            return true;
        case R.id.subItem1:
            Toast.makeText(this, "Sub Menu item 1 tapped", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.subItem2:
            Toast.makeText(this, "Sub Menu item 2 tapped", Toast.LENGTH_SHORT).show();
            return true;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}
4

1 回答 1

0
  1. 使用这个库:https ://github.com/JakeWharton/Android-ViewPagerIndicator ,你可以在这个库中找到一些带有你想要的标签的示例,并与 sherlockactionbar 一起使用。
  2. 访问这个问题:Actionbarsherlock + 选项卡 + 多片段?.
  3. 问我你有没有麻烦。
于 2013-01-29T13:36:13.537 回答