1、布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".activity.MainActivity"> <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="@+id/tabhost" android:layout_width="fill_parent" android:background="@drawable/ic_tab_bg" android:layout_height="wrap_content"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
2、activy中使用
定义fragment,名称和图片的数组
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); int count = mfragmets.length; for (int i = 0; i < count; i++) { TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]) .setIndicator(getTabItemView(i)); mTabHost.addTab(tabSpec, mfragmets[i], null); } mTabHost.getTabWidget().setDividerDrawable(null); mTabHost.setOnTabChangedListener(this);
@Override
public void onTabChanged(String tabId) { final int size = mTabHost.getTabWidget().getTabCount(); for (int i = 0; i < size; i++) { View v = mTabHost.getTabWidget().getChildAt(i); if (i == mTabHost.getCurrentTab()) { v.setSelected(true); } else { v.setSelected(false); } } }通过以上方法获取选中状态