在屏幕上方是actionBar tab的ViewPager页面里面嵌套子tab并显示于屏幕下方


参考了http://simplehappy.iteye.com/blog/1783845,用android-support-v13里面FragmentTabHost实现,代码片段:

public class TabHostFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View v = inflater.inflate(R.layout.main_linear, container, false);                FragmentTabHost fragmentTabHost = (FragmentTabHost) v.findViewById(R.id.fragmentTabHosts);        fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabContent);                fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Simple").setIndicator("Simple"),                CountingFragment.class, null);        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("List").setIndicator("List"),                FragmentPagerSupport.ArrayListFragment.class, null);        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Cursor").setIndicator("Cursor"),                CursorFragment.class, null);                return v;    }}
main_linear.xml,需要声明android.support.v13.app.FragmentTabHost的控件:

<?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" >    <FrameLayout        android:id="@+id/tabContent"        android:layout_width="match_parent"        android:layout_height="0dip"        android:layout_weight="1" />    <android.support.v13.app.FragmentTabHost        android:id="@+id/fragmentTabHosts"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>
其中 FragmentTabHost.setup( Context context, FragmentManager manager, int containerId)的containerId对应的view就是要显示的tab的内容,用android:layout_weight="1"方式让其把tabHost挤到下方(其在垂直布局中在tabHost上方),这样达到tab选项卡在下方显示的效果。推荐第一层tab用ActionBar,效应类似firefox的浏览tab标签,对于传统的tab实现方式,多个tab标签是平均分掉屏幕宽度的,当tab数比较多时显得非常拥挤。而ActionBar实现的tab,多出来的tab先不显示。可逐渐拖动出来显示,同时显示过的tab又被移动到了不可见区域。

上面代码里的fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabContent)的第二个参数必须是childFragmentManager,但是问题来了,在4.2平台之前,android.app.Fragment是没有getChildFragmentManager()方法的,(之前我就是在4.2的平台下编译,再放到4.0的模拟器上运行,出现了NoSuchMethodError,放到4.2的模拟器上运行就可以)。

在4.2之前的手机上要通过上述方式实现的话,要用android.support.v4.app.Fragment,这个类才有getChildFragmentManager()方法,该方法返回类型是android.support.v4.app.Fragment,导致要做出一系列的改动,用的FragmentTabHost就不是v13的了,是v4的,包括显示页的Activity也只能用android.support.v4.app.FragmentActivity,FragmentPagerAdapter也要v4的,细节可参考代码:http://download.csdn.net/download/tedzyc/5050980(V4.2的版本用New Android projects from existing code导入)。





更多相关文章

  1. Android 用代码获取基站号(cell)和小区号(lac)
  2. Android调节屏幕亮度工具类BrightnessUtils
  3. Android第一行代码(第二版)
  4. android手机通讯录备份还原代码
  5. 详解Android获取系统内核版本的方法与实现代码
  6. android屏幕单位
  7. Android中获取当前屏幕的尺寸大小
  8. android典型代码系列(二十八)------通话记录的操作

随机推荐

  1. Android实现图标水印
  2. Word文档的读取,WordToHtml(Android)
  3. Android中Intent传递对象的两种方法(Seri
  4. Android安全警告:发现高度复杂的Android木
  5. Android——TextView属性XML详解
  6. 在Ubuntu中和Android中添加开机自启动的
  7. android颜色关于Android(安卓)TabHost切
  8. 打造android ORM框架opendroid(二)——自
  9. Android-1
  10. Android(安卓)从启动到程序运行发生的事