在Android 3.0以后 Tabhost被标记为Deprecated,官方建议我们使用FragmentTabHost,这里用FragmentTabHost +Fragment 来实现之前已经实现过的 新浪微博布局效果。


关于FragmentTabHost 的说明可以参考http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html





maintabs.xml

<android.support.v4.app.FragmentTabHost 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" >        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:visibility="gone"/>        <FrameLayout            android:id="@+id/realtabcontent"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1" />        <TabWidget            android:id="@android:id/tabs"            android:layout_width="fill_parent"              android:layout_height="wrap_content"              android:layout_weight="0.0"              android:visibility="gone"/>        <RelativeLayout            android:id="@+id/layout_bottom"            android:layout_width="fill_parent"            android:layout_height="wrap_content" >            <RadioGroup                android:id="@+id/radiogroup"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_gravity="bottom"                android:background="@drawable/tabbar_background"                android:gravity="center_vertical"                android:orientation="horizontal" >                <RadioButton                    android:id="@+id/radio_home"                    style="@style/maintabt"                    android:drawableTop="@drawable/icon_home"                    android:text="主页" />                <RadioButton                    android:id="@+id/radio_msg"                    style="@style/maintabt"                    android:drawableTop="@drawable/icon_meassage"                    android:text="消息" />                <RadioButton                    android:id="@+id/radio_profile"                    style="@style/maintabt"                    android:drawableTop="@drawable/icon_profile"                    android:text="我" />                <RadioButton                    android:id="@+id/radio_square"                    style="@style/maintabt"                    android:drawableTop="@drawable/icon_square"                    android:text="广场" />                <RadioButton                    android:id="@+id/radio_more"                    style="@style/maintabt"                    android:drawableTop="@drawable/icon_more"                    android:text="更多" />            </RadioGroup>        </RelativeLayout>    </LinearLayout></android.support.v4.app.FragmentTabHost>



FragmentTabsActivity.java

public class FragmentTabsActivity extends FragmentActivity implementsOnCheckedChangeListener {private static final String TAG = FragmentTabsActivity.class.getSimpleName();private FragmentTabHost mTabHost;private RadioGroup radioGroup;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.maintabs);findView();}private void findView() {mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);mTabHost.getTabWidget().setVisibility(View.GONE); // 隐藏系统的TabWidgetmTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"),HomeFragment.class, null);mTabHost.addTab(mTabHost.newTabSpec("message").setIndicator("Message"),MessageFragment.class, null);mTabHost.addTab(mTabHost.newTabSpec("profile").setIndicator("Profile"),ProfileFragment.class, null);mTabHost.addTab(mTabHost.newTabSpec("square").setIndicator("Square"),SquareFragment.class, null);mTabHost.addTab(mTabHost.newTabSpec("more").setIndicator("More"),MoreFragment.class, null);// mTabHost.setOnTabChangedListener(this);mTabHost.setCurrentTabByTag("home");((RadioButton) findViewById(R.id.radio_home)).setChecked(true);radioGroup = (RadioGroup) findViewById(R.id.radiogroup);radioGroup.setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {FragmentManager fm = getSupportFragmentManager();HomeFragment homeFragment = (HomeFragment) fm.findFragmentByTag("home");MessageFragment msgFragment = (MessageFragment) fm.findFragmentByTag("message");ProfileFragment profileFragment = (ProfileFragment) fm.findFragmentByTag("profile");SquareFragment squareFragment = (SquareFragment) fm.findFragmentByTag("square");MoreFragment moreFragment = (MoreFragment) fm.findFragmentByTag("more");FragmentTransaction ft = fm.beginTransaction();// ** Detaches the androidfragment if exists */if (homeFragment != null)ft.detach(homeFragment);if (msgFragment != null)ft.detach(msgFragment);if (profileFragment != null)ft.detach(profileFragment);if (squareFragment != null)ft.detach(squareFragment);if (moreFragment != null)ft.detach(moreFragment);switch (checkedId) {case R.id.radio_home:if (homeFragment == null) {/** Create AndroidFragment and adding to fragmenttransaction */ft.add(R.id.realtabcontent, new HomeFragment(), "home");} else {/** * Bring to the front, if already exists in the * fragmenttransaction */ft.attach(homeFragment);}mTabHost.setCurrentTabByTag("home");break;case R.id.radio_msg:if (msgFragment == null) {ft.add(R.id.realtabcontent, new MessageFragment(), "message");} else {ft.attach(msgFragment);}mTabHost.setCurrentTabByTag("message");break;case R.id.radio_profile:if (profileFragment == null) {ft.add(R.id.realtabcontent, new ProfileFragment(), "profile");} else {ft.attach(profileFragment);}mTabHost.setCurrentTabByTag("profile");break;case R.id.radio_square:if (squareFragment == null) {ft.add(R.id.realtabcontent, new SquareFragment(), "square");} else {ft.attach(squareFragment);}mTabHost.setCurrentTabByTag("square");break;case R.id.radio_more:if (moreFragment == null) {ft.add(R.id.realtabcontent, new MoreFragment(), "more");} else {ft.attach(moreFragment);}mTabHost.setCurrentTabByTag("more");break;default:break;}ft.commitAllowingStateLoss();}}

FragmentTabHost 用法和TabHost 用法差不多,自己看看官方文档就明白了。


工程下载地址:http://download.csdn.net/detail/fx_sky/6608547


更正:

onCheckedChanged方法里面,最后是需要ft.commit();这句话的,由于个人马虎给大家造成误解表示道歉。完整代码如下:

@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {FragmentManager fm = getSupportFragmentManager();HomeFragment homeFragment = (HomeFragment) fm.findFragmentByTag("home");MessageFragment msgFragment = (MessageFragment) fm.findFragmentByTag("message");ProfileFragment profileFragment = (ProfileFragment) fm.findFragmentByTag("profile");SquareFragment squareFragment = (SquareFragment) fm.findFragmentByTag("square");MoreFragment moreFragment = (MoreFragment) fm.findFragmentByTag("more");FragmentTransaction ft = fm.beginTransaction();// ** Detaches the androidfragment if exists */if (homeFragment != null)ft.detach(homeFragment);if (msgFragment != null)ft.detach(msgFragment);if (profileFragment != null)ft.detach(profileFragment);if (squareFragment != null)ft.detach(squareFragment);if (moreFragment != null)ft.detach(moreFragment);switch (checkedId) {case R.id.radio_home:if (homeFragment == null) {/** Create AndroidFragment and adding to fragmenttransaction */ft.add(R.id.realtabcontent, new HomeFragment(), "home");} else {/** * Bring to the front, if already exists in the * fragmenttransaction */ft.attach(homeFragment);}mTabHost.setCurrentTabByTag("home");break;case R.id.radio_msg:if (msgFragment == null) {ft.add(R.id.realtabcontent, new MessageFragment(), "message");} else {ft.attach(msgFragment);}mTabHost.setCurrentTabByTag("message");break;case R.id.radio_profile:if (profileFragment == null) {ft.add(R.id.realtabcontent, new ProfileFragment(), "profile");} else {ft.attach(profileFragment);}mTabHost.setCurrentTabByTag("profile");break;case R.id.radio_square:if (squareFragment == null) {ft.add(R.id.realtabcontent, new SquareFragment(), "square");} else {ft.attach(squareFragment);}mTabHost.setCurrentTabByTag("square");break;case R.id.radio_more:if (moreFragment == null) {ft.add(R.id.realtabcontent, new MoreFragment(), "more");} else {ft.attach(moreFragment);}mTabHost.setCurrentTabByTag("more");break;default:break;}ft.commitAllowingStateLoss();}






更多相关文章

  1. Android(安卓)官方自带的Edittext悬浮标签
  2. 报错记录-The option ‘android.enableUnitTestBinaryResources
  3. android 简单实现,微信第三方登录
  4. ADT Plugin for Eclipse(Android开发工具) 20.0.3 官方最新版
  5. 新浪下载图片的ProgressBar进度样式源码
  6. Android错误解决方法之:Debug certificate expired on
  7. Android官方架构组件ViewModel+LiveData+DataBinding架构属于自
  8. 使用TelephonyManager类获取手机状态
  9. Android下载文件常见错误解决方法

随机推荐

  1. Android――Android(安卓)lint工具项目资
  2. Android SAX和DOM解析XML文件
  3. 个人安卓学习笔记---第一个Android应用程
  4. Android Context 使用时注意内存泄漏问题
  5. android的帮助、about、关于作者、HELP等
  6. Android(安卓)程序完全退出的几种方法(参
  7. Android 知识汇总
  8. Android增加键值(二)
  9. Android入门(4) 开发第一个Android程序
  10. 给android添加系统属性:Settings.system和