Tabhost与Fragment的结合使用

主界面布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>
tab_item_view布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center">
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:padding="3dp"
        android:src="@drawable/tab_home_pre">
    </ImageView>

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="首页">
    </TextView>
</LinearLayout>
主Activity中的部分代码:

package com.activity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.beidougd.bdgc.R;
import com.umeng.update.UmengUpdateAgent;


public class FragmentMainActivity extends FragmentActivity {
    private ImageView imageView;
	private TextView textView;
	private long touchTime = 0;
	private static FragmentActivity activity;
	//定义FragmentTabHost对象
	private FragmentTabHost mTabHost;

	//定义一个布局
	private LayoutInflater layoutInflater;

	//定义数组来存放Fragment界面
	private Class fragmentArray[] = {TabHome.class,TabHistory.class,TabFind.class,TabSetting.class};

	//定义数组来存放按钮图片
	private int mImageViewArray[] = {R.drawable.tab_home_selector,R.drawable.tab_history_selector,R.drawable.tab_find_selector,
			R.drawable.tab_setting_selector};

	//Tab选项卡的文字
	private String mTextviewArray[] = {"首页", "历史", "发现", "设置"};
	@SuppressLint("NewApi") 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		activity = this;
		initViews();
		UmengUpdateAgent.update(this);
		//接收通知打开历史界面
		getIntentandopenHistory();
	}

	private void initViews() {
		MyApplication.getIntance().addActivity(FragmentMainActivity.this);
		//实例化布局对象
		layoutInflater = LayoutInflater.from(this);

		//实例化TabHost对象,得到TabHost
		mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

		//得到fragment的个数
		int count = fragmentArray.length;
		for(int i = 0; i < count; i++) {
			//为每一个Tab按钮设置图标、文字和内容
			TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
			//将Tab按钮添加进Tab选项卡中
			mTabHost.addTab(tabSpec, fragmentArray[i], null);
点击下载上述完整代码块。


点击下载完整项目代码。


你可能感兴趣的:(Tabhost与Fragment的结合使用)