Tab布局是iOS的经典布局,Android应用中也有大量应用,前面也写过Android中TAb的实现,《Android UI开发第十八篇——ActivityGroup实现tab功能》。这篇文章总结几种Tab的实现。

1)继承自TabActivity,TabActivity已在API Level 13中不建议使用,所有作者不建议在新开发的应用中使用TabActivity,关于Tabactivity的一些分析可以参考Android TabActivity之感叹,英雄暮路,美人辞暮,昔日的英雄再见》

2)继承自Activity,布局使用TabHost,这个其实和上面的TabActivity一样,只不过是这里使用findViewById( )获取TabHost对象,而TabActivity使用getTabHost()获得TabHost对象。

3)使用ActivityGroup,《Android UI开发第十八篇——ActivityGroup实现tab功能》中有介绍,当然,下面的button布局会有更多的实现方式,各位也可以自己扩展。ActivityGroup也在API Level 13中不建议使用。

4)使用ActionBar的ActionBar.NAVIGATION_MODE_TABS模式,参考ActionBar的添加导航选项标签介绍。ActionBar是Android 3.0后出现的,为了兼容更多的Android版本,可以参考《Android UI开发第三十五篇——AppCompat实现Action Bar》。

5)继承自FragmentActivity,使用FragmentTabHost。具体实现可以参考FragmentTabHost。当然也可以直接继承自Fragment,使用FragmentTabHost。

6)可以制作更高级的Tab界面,手势滑动的Tab,参考《Creating Swipe Views With Tabs》. 手势滑动切换Tab可以使用ViewPage单独实现。

7)实现Tab界面的方法有很多,每个开发者都可能有不同的方法,尤其对付Tab的下面的Button,开发者可以各种自定义,我见过使用Button实现的也有使用ImageView实现的,也有RadioButton实现的,也有混合布局实现的,这些可以充分发挥开发者想象力。

自TabActivity过期,不建议使用,很多开发者试图把原先的TabActivity修改为FragmentTabHost,使用官方提供的方法不能把Tab的button放到下面。

  1. <android.support.v4.app.FragmentTabHost
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. <TabWidget
  11. android:id="@android:id/tabs"
  12. android:orientation="horizontal"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_weight="0"/>
  16. <FrameLayout
  17. android:id="@android:id/tabcontent"
  18. android:layout_width="0dp"
  19. android:layout_height="0dp"
  20. android:layout_weight="0"/>
  21. <FrameLayout
  22. android:id="@+id/realtabcontent"
  23. android:layout_width="match_parent"
  24. android:layout_height="0dp"
  25. android:layout_weight="1"/>
  26. </LinearLayout>
  27. </android.support.v4.app.FragmentTabHost>

理论上,我们修改一下上面的布局即可使实现tab的button下面。

  1. <android.support.v4.app.FragmentTabHost
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. <FrameLayout
  11. android:id="@android:id/tabcontent"
  12. android:layout_width="0dp"
  13. android:layout_height="0dp"
  14. android:layout_weight="0"/>
  15. <FrameLayout
  16. android:id="@+id/realtabcontent"
  17. android:layout_width="match_parent"
  18. android:layout_height="0dp"
  19. android:layout_weight="1"/>
  20. <TabWidget
  21. android:id="@android:id/tabs"
  22. android:orientation="horizontal"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:layout_weight="0"/>
  26. </LinearLayout>
  27. </android.support.v4.app.FragmentTabHost>

我们改变了布局以后,发现我错了,显示没有变化,最终发现是FragmentTabHost的一个bug。参考

I change TabWidget down, but it will never be used xml in FragmentTabHost.

I search Bug in support.v4 library:
Copy FragmentTabHost.java to your own project and Comment line 153 to 175 or change initFragmentTabHost:

 private void initFragmentTabHost(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
new int[] { android.R.attr.inflatedId }, 0, 0);
mContainerId = a.getResourceId(0, 0);
a.recycle(); super.setOnTabChangedListener(this); // If owner hasn't made its own view hierarchy, then as a convenience
// we will construct a standard one here. /***** HERE COMMENT CODE BECAUSE findViewById(android.R.id.tabs) EVERY TIME IS NULL WE HAVE OWN LAYOUT ******// // if (findViewById(android.R.id.tabs) == null) {
// LinearLayout ll = new LinearLayout(context);
// ll.setOrientation(LinearLayout.VERTICAL);
// addView(ll, new FrameLayout.LayoutParams(
// ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.FILL_PARENT));
//
// TabWidget tw = new TabWidget(context);
// tw.setId(android.R.id.tabs);
// tw.setOrientation(TabWidget.HORIZONTAL);
// ll.addView(tw, new LinearLayout.LayoutParams(
// ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.WRAP_CONTENT, 0));
//
// FrameLayout fl = new FrameLayout(context);
// fl.setId(android.R.id.tabcontent);
// ll.addView(fl, new LinearLayout.LayoutParams(0, 0, 0));
//
// mRealTabContent = fl = new FrameLayout(context);
// mRealTabContent.setId(mContainerId);
// ll.addView(fl, new LinearLayout.LayoutParams(
// LinearLayout.LayoutParams.FILL_PARENT, 0, 1));
// }
}

这样解决了使用FragmentTabHost实现Tab界面button不能在下面的问题。

上面提到的Tab界面的实现方式,归根结底也就分成两大类,前三个为一类,我们统称为Old Tab;后三个为一类,我们统称New Tab。Old Tab可以加载Activity,New Tab加载的是Fragment,这时他们的生命周期是不同的。Old Tab加载的Activtiy再次回来时是从onResume开始的,而New Tab加载的Fragment(Fragment的生命周期)再次回来时是从 onCreateView开始的。

实现Tab界面不止上面介绍的,因为众多开发者的创造力是无穷的,希望这篇能起到抛砖引玉的功效。

/**
* @author 张兴业
*  iOS入门群:83702688
*  android开发进阶群:241395671
*  我的新浪微博:@张兴业TBOW
*/

参考:

http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

http://developer.android.com/training/implementing-navigation/lateral.html

Android UI开发第三十九篇——Tab界面实现汇总及比较的更多相关文章

  1. Android UI开发第三十五篇——AppCompat实现Action Bar

    每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了.Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计. ...

  2. Android UI开发第三十二篇——Creating a Navigation Drawer

    Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...

  3. Android UI开发第三十四篇——SlidingPaneLayout

    SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能.它支持左右滑动菜 ...

  4. Android UI开发第三十六篇——使用Volley加载图片列表

    Android开发者可能会使用Universal Image Loader或者Square`s newer Picasso这些第三方的库去处理图片的加载,那么Volley是怎么加载图片列表的呢,这一篇 ...

  5. Android UI开发第三十篇——使用Fragment构建灵活的桌面

    http://www.lupaworld.com/article-222973-1.html 当我们设计应用程序时,希望能够尽最大限度的适配各种设备,包括4寸屏.7寸屏. 10寸屏等等,Android ...

  6. Android UI开发第四十二篇——实现实现易信的圆形图像和对话列表的图像显示部分

    显示图像时,很多个性化显示,圆形或圆角.气泡等等,我们这一篇文章探讨一下圆形和气泡的显示,仿照易信中的实现,先看下效果图: 代码: public class RoundImageView extend ...

  7. Android UI开发第三十一篇——Android的Holo Theme

    好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...

  8. 【转】Android UI开发第三十一篇——Android的Holo Theme

    好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...

  9. Android UI开发第三十三篇——Navigation Drawer For Android API 7

    Creating a Navigation Drawer中使用的Navigation Drawer的android:minSdkVersion="14",现在Android API ...

随机推荐

  1. spoj 694 求一个字符串中不同子串的个数

    SPOJ Problem Set (classical) 694. Distinct Substrings Problem code: DISUBSTR Given a string, we need ...

  2. 【PHP】组合条件搜索SQL

    前端html多个搜索条件组合 后台一个sql语句,很方便和简洁:仅提供思路. 也可以配合着进行分页操作,非常赞~

  3. 网页中font-family的属性解析

    web中文字体的选择 web应用程序因其跨平台性被广泛应用,但是也为web应用程序运行带来了复杂的运行环境,比如各个系统字体的区别以及中英文字体显示的区别. 字体分类 网页常用字体通常分为5类:ser ...

  4. 使用Parallel实现简单的并行操作

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  5. php_memcahed 使用方法

    用php_memcache.dll 扩展库操作方法 1.下载php_memcache.dll 对应的PHP版本拷贝到PHP目录EXT下 2.在php.ini添加扩展extension=php_memc ...

  6. Unity3D刚体不同力的测试(ForceMode,AddForce,RelativeAddForce)

    摘自圣典的一段翻译: ForceAdd a continuous force to the rigidbody, using its mass.添加一个可持续力到刚体,使用它的质量.Accelerat ...

  7. delete void *

    看代码的时候看到 typedef char xxx[sizeof(T) ? 1 : -1]; 这种语句 如:JavaScriptCore/wtf/OwnPtrCommon.h template < ...

  8. 第四篇: python函数续

    1.函数的嵌套 函数的嵌套调用:在调用一个函数的过程中,又调用了其它函数 示例1: def bar(): print('from nbar') def foo(): print('from foo') ...

  9. PHP QR Code - QR code generator, an LGPL PHP library

    http://phpqrcode.sourceforge.net/examples/index.php http://phpqrcode.sourceforge.net/examples/index. ...

  10. vue render函数 函数组件化

    之前创建的锚点标题组件是比较简单,没有管理或者监听任何传递给他的状态,也没有生命周期方法,它只是一个接受参数的函数 在这个例子中,我们标记组件为functional,这意味它是无状态(没有data), ...