总结开发使用tabHost+Fragment的使用步骤:

1、在Activity的布局文件中使用TabHost
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent"     >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >                <!-- Tabwidget的位置决定了tabhost选项卡的位置,移动到父控件的第一个选项卡在上,反之成立 -->        <!-- 这是整个选项卡 -->        <!-- tabStripEnabled选项卡底部没用的部分 -->        <!-- divider 去除选项卡之间的分割线 -->        <TabWidget            android:id="@android:id/tabs"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:background="#565656"            android:tabStripEnabled="false"            android:divider="@null"            />        <FrameLayout            android:id="@+id/realtabcontent"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1" />        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0" />    </LinearLayout></TabHost>


2、初始化TabHost控件,并找到每一个选项卡
                tabHost = (TabHost) findViewById(android.R.id.tabhost);TabWidget tw = (TabWidget) ((LinearLayout) tabHost.getChildAt(0)).getChildAt(TAB_UP);for(int i = 0 ; i < mTabtexts.length ; i ++){LinearLayout tab = (LinearLayout) getLayoutInflater().inflate(R.layout.tab_indicator, tw, false);((ImageView)tab.getChildAt(0)).setImageResource(mImages[i]);((TextView)tab.getChildAt(1)).setText(mTabtexts[i]);mTabs.add(tab);}                tabHost.setup();//这一步必须有tabHost.setOnTabChangedListener(changeListener);


3、添加选项卡的内容
                int i = 0 ;for(LinearLayout tab : mTabs)tabHost.addTab(tabHost.newTabSpec(mTags[i++]).setIndicator(tab).setContent(new TabContent(this)));


4、然后就是选项卡的切换
@Overrideprotected void tabChange(String tabId) {boy = (BoyFragment) manager.findFragmentByTag(mTags[0]);girl = (GirlFragment) manager.findFragmentByTag(mTags[1]);/** FragmentTransaction 在使用之前使用beginTransaction,每一次使用之后需要commit */transaction = manager.beginTransaction();detachAll();toAttach(tabId);// 使用这个方法提交会有一个问题,当外部调用tabhost.setCurrentTab()时,会报错 */transaction.commitAllowingStateLoss();}private void detachAll(){if (boy != null)transaction.detach(boy);if (girl != null)transaction.detach(girl);}private void toAttach(String tag) {if(mTags[0].equals(tag)){if (boy == null) {transaction.add(R.id.realtabcontent, new BoyFragment(), tag); /** 这里创建Fragment对象,并且绑定tag */} else {transaction.attach(boy);}}else if(mTags[1].equals(tag)){if (girl == null) {transaction.add(R.id.realtabcontent, new GirlFragment(), tag);} else {transaction.attach(girl);}}}

更多相关文章

  1. Android 使用WebView控件展示SVG图
  2. 各种控件属性
  3. 系出名门Android(6) - 控件(View)
  4. andriod布局常用控件
  5. android 自定义控件属性
  6. Android图表控件MPAndroidChart——曲线图LineChart(多条曲线)动态
  7. android UI开发及常用控件
  8. Android图表控件MPAndroidChart——曲线图LineChart的使用(多条曲

随机推荐

  1. 认清Android(安卓)Studio下的Gradle是什
  2. Android真机测试,连接到本地服务器的方法
  3. Android导出lib时资源id的问题
  4. Android USB串口开发
  5. Android(安卓)使用 aidl 文件创建服务示
  6. Android(安卓)JNI配置及入门
  7. Android Native Crash 捕获之 BreakPad
  8. android app耗电分析方法
  9. Android IPC进程间通信,Binder机制原理及A
  10. Android行驶证离线识别sdk接口