我的tabhost不起作用

时间:2015-05-02 05:00:00

标签: android android-tabhost

我正在使用带有2个选项卡的tabhost我在选项卡活动中无法解决任何问题,

这是我实现标签主机

的主要类
tabHost = getTabHost();
//*   TabHost Implementations*//*
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();

TabSpec tab1 = tabHost.newTabSpec("FirstTab");
TabSpec tab2 = tabHost.newTabSpec("SecondTab");

// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Friends");
Intent tab1intent = new Intent().setClass(this, Friends_list_add.class);
tab1intent.putExtra("userid", userid);
System.out.println("tabHost------" + tab1intent);
tab1.setContent(tab1intent);

tab2.setIndicator("Invite Friends");
Intent tab2intent = new Intent().setClass(this, Friends_Invite_list.class);
tab2intent.putExtra("response", response);
System.out.println("tab2intent------" + tab2intent);
tab2.setContent(tab2intent);


//** Add the tabs  to the TabHost to display. *//*
tabHost.addTab(tab1);
tabHost.addTab(tab2);

tab = 1;
//set Windows tab as default (zero based)
tabHost.setCurrentTab(tab);

通过意图,我将在onCreate

中切换到该活动中的另一个活动

我必须按照以下textview查看布局,但它不显示任何内容

no_friends = (TextView) findViewById (R.id.nodata_found_friends);    

我已经推荐了所有的网站,但仍然没有得到任何东西,        请帮助我,提前致谢

2 个答案:

答案 0 :(得分:0)

第一个代码放在xml文件

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/title_back_color" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="6" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.10"
            android:background="@color/popular_back_color"
            android:orientation="horizontal"
            android:showDividers="none"
            android:tabStripEnabled="false" />
    </LinearLayout>
</TabHost>

现在在您的主要活动中放置此代码。

public class MainActivity extends FragmentActivity implements
    OnTabChangeListener, OnPageChangeListener {

MyPageAdapter pageAdapter;
private ViewPager mViewPager;
private TabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);

    // Tab Initialization
    initialiseTabHost();

    // Fragments and ViewPager Initialization
    List<Fragment> fragments = getFragments();
    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
    mViewPager.setAdapter(pageAdapter);
    mViewPager.setOnPageChangeListener(MainActivity.this);
}

// Method to add a TabHost
private static void AddTab(MainActivity activity, TabHost tabHost,
        TabHost.TabSpec tabSpec) {
    tabSpec.setContent(new MyTabFactory(activity));
    tabHost.addTab(tabSpec);
}

// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
    int pos = this.mTabHost.getCurrentTab();
    this.mViewPager.setCurrentItem(pos);

    setSelectedTabColor();
}

@Override
public void onPageScrollStateChanged(int arg0) {
}

// Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    int pos = this.mViewPager.getCurrentItem();
    this.mTabHost.setCurrentTab(pos);
}

@Override
public void onPageSelected(int arg0) {
}

@SuppressLint("ResourceAsColor")
private void setSelectedTabColor() {
    for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {

        TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i)
                .findViewById(android.R.id.title);
        tv.setTextColor(getResources().getColor(R.color.white));

        mTabHost.getTabWidget()
                .getChildAt(i)
                .setBackgroundColor(
                        getResources().getColor(R.color.popular_back_color));
    }
    mTabHost.getTabWidget()
            .getChildAt(mTabHost.getCurrentTab())
            .setBackgroundColor(
                    getResources().getColor(R.color.app_yellow_color));
}

private List<Fragment> getFragments() {
    List<Fragment> fList = new ArrayList<Fragment>();

    // TODO Put here your Fragments
    // DealTab f1 = DealTab.newInstance();
    DealTab_New f1 = DealTab_New.newInstance();
    EventTab f2 = EventTab.newInstance();

    MyAccountFragment f3 = MyAccountFragment.newInstance();
    MessageFragment f4 = MessageFragment.newInstance();
    MoreFragment f5 = MoreFragment.newInstance();
    QrCodeFragment f6 = QrCodeFragment.newInstance();

    // fList.add(f1);
    fList.add(f1);
    fList.add(f2);
    fList.add(f3);
    fList.add(f4);
    fList.add(f5);
    fList.add(f6);

    return fList;
}

// Tabs Creation
@SuppressLint("ResourceAsColor")
private void initialiseTabHost() {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    // TODO Put here your Tabs
    MainActivity.AddTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Tab1").setIndicator(
                    "",
                    getApplicationContext().getResources().getDrawable(
                            R.drawable.btn_deals)));

    MainActivity.AddTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Tab2").setIndicator(
                    "",
                    getApplicationContext().getResources().getDrawable(
                            R.drawable.btn_event)));
    MainActivity.AddTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Tab3").setIndicator(
                    "",
                    getApplicationContext().getResources().getDrawable(
                            R.drawable.btn_my_account)));
    MainActivity.AddTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Tab4").setIndicator(
                    "",
                    getApplicationContext().getResources().getDrawable(
                            R.drawable.btn_message)));
    MainActivity.AddTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Tab5").setIndicator(
                    "",
                    getApplicationContext().getResources().getDrawable(
                            R.drawable.btn_more)));

    mTabHost.setOnTabChangedListener(this);

    setSelectedTabColor();
}

它的工作正常,我在我的应用程序中使用它。 谢谢&amp;问候。

答案 1 :(得分:0)

尝试这种方式使tabhost我确定它的工作。

&#13;
&#13;
public class MainActivity extends TabActivity implements OnTabChangeListener,
		OnClickListener {

	private ImageView iv_Home;
	private ImageView iv_Chat;
	private ImageView iv_Post;
	private ImageView iv_Alert;
	private ImageView iv_More;
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		Casting();
	}

	private void Casting() {
		// TODO Auto-generated method stub
		iv_Home = (ImageView) findViewById(R.id.iv_Home);
		iv_Chat = (ImageView) findViewById(R.id.iv_Chat);
		iv_Post = (ImageView) findViewById(R.id.iv_Post);
		iv_Alert = (ImageView) findViewById(R.id.iv_Alert);
		iv_More = (ImageView) findViewById(R.id.iv_More);

		iv_Home.setOnClickListener(this);
		iv_Chat.setOnClickListener(this);
		iv_Post.setOnClickListener(this);
		iv_Alert.setOnClickListener(this);
		iv_More.setOnClickListener(this);

		tabHost = getTabHost();
		tabHost.setup();

		TabHost.TabSpec tabSpec;

		tabSpec = tabHost.newTabSpec("Tab1").setIndicator("")
				.setContent(new Intent(this, CategoryActivity.class));
		tabHost.addTab(tabSpec);

		tabSpec = tabHost.newTabSpec("Tab2").setIndicator("")
				.setContent(new Intent().setClass(this, ChatActivity.class));
		tabHost.addTab(tabSpec);

		tabSpec = tabHost
				.newTabSpec("Tab3")
				.setIndicator("")
				.setContent(
						new Intent().setClass(this, LoginActivity.class));
		tabHost.addTab(tabSpec);

		tabSpec = tabHost.newTabSpec("Tab4").setIndicator("")
				.setContent(new Intent().setClass(this, AlertActivity.class));
		tabHost.addTab(tabSpec);

		tabSpec = tabHost.newTabSpec("Tab5").setIndicator("")
				.setContent(new Intent().setClass(this, MoreActivity.class));
		tabHost.addTab(tabSpec);

		tabHost.setOnTabChangedListener(this);
		tabHost.setCurrentTab(0);
		setSelectedTabColor();

	}

	@SuppressWarnings("unused")
	@Override
	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		int pos = this.tabHost.getCurrentTab();
		// this.mViewPager.setCurrentItem(pos);
		setSelectedTabColor();
	}

	// private static void AddTab(MainActivity activity, TabHost tabHost,
	// TabHost.TabSpec tabSpec) {
	// tabSpec.setContent(new CategoryTabFactory(activity));
	// tabHost.addTab(tabSpec);
	// }

	private void setSelectedTabColor() {
		// TODO Auto-generated method stub
		switch (tabHost.getCurrentTab()) {
		case 0:
			Global.setActionbar(this, getActionBar(),
					getString(R.string.app_name));
			iv_Home.setImageResource(R.drawable.btn_home_hover);
			iv_Chat.setImageResource(R.drawable.btn_chat);
			iv_Post.setImageResource(R.drawable.btn_post);
			iv_Alert.setImageResource(R.drawable.btn_alert);
			iv_More.setImageResource(R.drawable.btn_more);
			break;
		case 1:
			Global.setActionbar(this, getActionBar(), getString(R.string.chat));
			iv_Home.setImageResource(R.drawable.btn_home);
			iv_Chat.setImageResource(R.drawable.btn_chat_hover);
			iv_Post.setImageResource(R.drawable.btn_post);
			iv_Alert.setImageResource(R.drawable.btn_alert);
			iv_More.setImageResource(R.drawable.btn_more);
			break;
		case 2:
			Global.setActionbar(this, getActionBar(),
					getString(R.string.post_product));
			iv_Home.setImageResource(R.drawable.btn_home);
			iv_Chat.setImageResource(R.drawable.btn_chat);
			iv_Post.setImageResource(R.drawable.btn_post_hover);
			iv_Alert.setImageResource(R.drawable.btn_alert);
			iv_More.setImageResource(R.drawable.btn_more);
			break;
		case 3:
			Global.setActionbar(this, getActionBar(), getString(R.string.alert));
			iv_Home.setImageResource(R.drawable.btn_home);
			iv_Chat.setImageResource(R.drawable.btn_chat);
			iv_Post.setImageResource(R.drawable.btn_post);
			iv_Alert.setImageResource(R.drawable.btn_alert_hover);
			iv_More.setImageResource(R.drawable.btn_more);
			break;
		case 4:
			Global.setActionbar(this, getActionBar(), getString(R.string.more));
			iv_Home.setImageResource(R.drawable.btn_home);
			iv_Chat.setImageResource(R.drawable.btn_chat);
			iv_Post.setImageResource(R.drawable.btn_post);
			iv_Alert.setImageResource(R.drawable.btn_alert);
			iv_More.setImageResource(R.drawable.btn_more_hover);
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == iv_Home) {
			tabHost.setCurrentTab(0);
		} else if (v == iv_Chat) {
			tabHost.setCurrentTab(1);
		} else if (v == iv_Post) {
			tabHost.setCurrentTab(2);
		} else if (v == iv_Alert) {
			tabHost.setCurrentTab(3);
		} else if (v == iv_More) {
			tabHost.setCurrentTab(4);
		}
		setSelectedTabColor();
	}
}
&#13;
&#13;
&#13;