TabActivity和TabHost+Fragment和FragmentTabHost的区别

TabActivity在3.0以上就不推荐用了,原因不知,应该是内存管理不好。

TabHost+Fragment也不错,但是 会出现某个页面不出来的情况,也不用。

FragmentTabHost 就比较好, 

第一步:继承FragmentActivity,第二步:定义xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    //①这里放个 标题栏
    <include layout="@layout/pre_titlebar_layout" />

    //②这里页面的内容
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >
    </FrameLayout>
    //③这里放 TabHost,TabHost是个FragmentLayout结构,
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/size50"
         >
         <RelativeLayout 
             android:orientation="vertical"
             android:layout_height="match_parent"
             android:layout_width="match_parent">
			<View 
			    android:layout_width="match_parent"
			    android:layout_height="1dp"
			    android:background="#ddd"
			    android:layout_alignParentTop="true"/>
			<!-- 给它设置tabwidget,,,,可是它还是用内置的。。。。framelayout -->
<!-- 			<TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="48dp"
                android:gravity="center"
                 /> -->
			<View 
			    android:id="@+id/v_tabIndicator"
			    android:layout_width="0dp"
			    android:layout_height="2dp"
			    android:layout_alignParentBottom="true"
			    android:background="@color/pregnancy_tv_color_common"/>
         </RelativeLayout>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>


第三步,写java中代码:

// 设置FragmentTabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
LinearLayout.LayoutParams params = (LayoutParams) mTabHost.getTabWidget().getLayoutParams();
// Log.e(TAG,
// "tabweiget的属性,width:"+params.width+"--height:"+params.height+"--gravity:"+params.gravity);
params.gravity = Gravity.CENTER;
params.height = LinearLayout.LayoutParams.MATCH_PARENT; // 默认是宽
// match_parent,高
// wrap_content
mTabHost.getTabWidget().setLayoutParams(params);

/** Defining tab builder for tab */
for (int i = 0; i < mTextArray.length; i++) {
TabHost.TabSpec tSpec = mTabHost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));
mTabHost.addTab(tSpec, fragmentArray[i], null);
mTabHost.getTabWidget().getChildAt(i).setOnTouchListener(new MyTouchListener(i));
}

// 设置page状态改变的监听。
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
// 第一次默认为0是不会调用onTabChanged的
@Override
public void onTabChanged(String tabId) {
int tabIndex = 0;
if (tabId.equals(mTextArray[0])) {
tabIndex = 0;
btn_ok.setVisibility(View.VISIBLE);
btn_add.setVisibility(View.GONE);
} else if (tabId.equals(mTextArray[1])) {
if(JTool.isEmpty(TempAll.getTempAll().getFuserid())){ //登录检测
mToLoginDialog.show();
mTabHost.setCurrentTab(mCurrTabIndex);
return;
}

tabIndex = 1;
btn_ok.setVisibility(View.GONE);
btn_add.setVisibility(View.VISIBLE);
tv_headertitle.setText("我的心情");
btn_add.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
/*Bimp.bmp.clear();*/
Intent Intent = new Intent(
PreNaviHomeActivity.this,
PublishedActivity.class);
startActivity(Intent);
}
});
} else if (tabId.equals(mTextArray[2])) {
tabIndex = 2;
btn_ok.setVisibility(View.GONE);
btn_add.setVisibility(View.GONE);
} else if (tabId.equals(mTextArray[3])) {
tabIndex = 3;
btn_ok.setVisibility(View.GONE);
btn_add.setVisibility(View.GONE);
} else {
return;
}


onTabPageChanged(tabIndex);
if (myTabChangeListeners == null)
return;
Log.e(TAG, "当前注册监听器个数:" + myTabChangeListeners.size());
Iterator<MyTabChangeNotifyListener> iterator = myTabChangeListeners.iterator();
while (iterator.hasNext()) {
MyTabChangeNotifyListener listener = iterator.next();
if (listener != null)
listener.onTabChange(tabIndex);
else
iterator.remove();
}
}
});


// 初始状态下tab1被选中且不会触发 监听事件。
setSlctState(0);


好了,有时间再补充,先mark

你可能感兴趣的:(TabActivity和TabHost+Fragment和FragmentTabHost的区别)