由于业务需要,需要在使用Activity的顶部使用一个导航栏,点击导航栏的tab,下面显示内容。决定采用项目中已经使用过的FragmentTabHost + Fragment的方式实现。不同的是之前的FragmentTabHost位于底部(下面统称:Bottom),现在需要放置在顶部(下面统称:Top)。下面将对这两种方式进行比较:

Bottom:

  Activity布局文件:

    

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <FrameLayout
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"/> <android.support.v4.app.FragmentTabHost
android:id="@+id/tabHost_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<FrameLayout
android:id="@+id/tabContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.app.FragmentTabHost> </LinearLayout>

Activity中设置FragmentTabHost:

    protected void initTabs() {
fm = getSupportFragmentManager();
layoutInflater = LayoutInflater.from(this); tabLabels = new String[]{
CommonUtil.getStringFromRes(this, R.string.text_tab_promotion),
CommonUtil.getStringFromRes(this, R.string.text_tab_order),
CommonUtil.getStringFromRes(this, R.string.text_tab_personal)
}; tabHost.setup(this, fm, R.id.content_main);
for (int i=0; i<fragments.length; i++) {
TabHost.TabSpec tabSpec = tabHost.newTabSpec(tabLabels[i]).setIndicator(getTabItemView(i));
tabHost.addTab(tabSpec, fragments[i], null);
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.white);
}
tabHost.getTabWidget().setDividerDrawable(null);
}

显示正常


Top:

top方式Activity中对FragmentTabHost的设置跟上面一样,因此只对Layout做对比。

<?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"> <android.support.v4.app.FragmentTabHost
android:id="@+id/tab_host"
android:layout_width="match_parent"
android:layout_height="40dp"/> <FrameLayout
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>

结果为:

下面的内容,无论tab切换到哪一个,都是空白。针对这种情况,进行断点分析,发现Fragment中的View控件都已经被加载,只是被遮挡住了。

因此,问题应该出在布局上。

  偶然的将LinearLayout换成RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <FrameLayout
android:layout_marginTop="42dp"
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/> <android.support.v4.app.FragmentTabHost
android:id="@+id/tab_host"
android:layout_width="match_parent"
android:layout_height="40dp"/> </RelativeLayout>

成功:

这里为什么使用LinlearLayout不成功,换成RelativeLayout可以,最终的原因,待研究清楚再总结。

FragmentTabHost + Fragment 使用小记的更多相关文章

  1. android FragmentActivity+FragmentTabHost+Fragment框架布局

    这周比较闲,计划系统的学习一下android开发,我本是一名IOS程序员,对手机开发还是有自己的一套思路的, 固这套思路用到我当前学android上了,先选择从Main页面的tabbar部分代码入手, ...

  2. FragmentTabHostBottomDemo【FragmentTabHost + Fragment实现底部选项卡】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现底部选项卡效果. 备注:该Demo主要是演示FragmentTabHost的一些设置和部分功能 ...

  3. [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单

    Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...

  4. Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换

    一.问题描述 在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:http://www.cnblogs.com/jerehedu/p/460759 ...

  5. 21 FragmentTabHost +Fragment代码案例

    注意头导航标签过多会被压缩并 结构 MainActivity.java package com.qf.day21_fragmenttabhost_demo1; import com.qf.day21_ ...

  6. Android控件使用FragmentTabHost,切换Fragment;

    大部分APP的主界面都很类似,要么底部导航的,要么就是侧滑菜单,还有底部导航+侧滑菜单的:底部导航实现大概有几种方式: TabHost+Fragment RadioGroup+Fragment Fra ...

  7. 使用FragmentTabHost+TabLayout+ViewPager实现双层嵌套Tab

    大多数应用程序都会在底部使用3~5个Tab对应用程序的主要功能进行划分,对于一些信息量非常大的应用程序,还需要在每个Tab下继续划分子Tab对信息进行分类显示. 本文实现采用FragmentTabHo ...

  8. Android典型界面设计——ViewPage+Fragment实现区域顶部tab滑动切换

    一.问题描写叙述 本系列将结合案例应用,陆续向大家介绍一些Android典型界面的设计,首先说说tab导航,导航分为一层和两层(底部区块+区域内头部导航).主要实现方案有RadioGroup+View ...

  9. FragmentTabHostTopDemo【FragmentTabHost固定宽度且居中】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现顶部选项卡(居中且宽度非全屏)展现. 备注:该Demo主要是演示FragmentTabHost ...

随机推荐

  1. JokeClient-Swift 仿写学习

    required init?(coder aDecoder: NSCoder) 可失败构造器 在init关键字后面添加问号(init?). 可失败构造器会创建一个类型为自身类型的可选类型的对象.你通过 ...

  2. 【代码笔记】iOS-正在加载

    一,效果图. 二,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the ...

  3. [AlwaysOn Availability Groups]监控AG性能

    监控AG性能 AG的性能的性能方面,在关键任务数据库上进行语句级维护性能是很重要的.理解AG如何传输日志到secondary副本对评估RTO和RPO,表明AG是否性能不好. 1. 数据同步步骤 为了评 ...

  4. EF和MVC系列文章导航:EF Code First、DbContext、MVC

    对于之前一直使用webForm服务器控件.手写ado.net操作数据库的同学,突然来了EF和MVC,好多新概念泉涌而出,的确犹如当头一棒不知所措.本系列文章可以帮助新手入门并熟练使用EF和MVC,有了 ...

  5. how2heap分析系列:1_first_fit

    一些基础知识不再赘述,可以自行搜索解决 程序源码first_fit.c #include <stdio.h> #include <stdlib.h> #include < ...

  6. Arduino舵机控制

    普通舵机有3根线:GND(黑).VCC(红).Signal(黄) 红色的是电源正极,黑色的是电源负极,白色的是信号线.有些舵机线是红棕橘三色,分别对应红黑白. #include <Servo.h ...

  7. iOS开发常用代码块

    遍历可变数组的同时删除数组元素 NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array]; NSString *str1 = ...

  8. plain framework 1 1.0.4 更新 稳定版发布

    PF由于各种因素迟迟不能更新,此次更新主要是更新了以往和上个版本出现的内存问题,该版本较为稳定,如果有用到的朋友请更新至此版本. PF 1.0.4 修复1.0.0.3更新后产生的内存问题,可能导致网络 ...

  9. 【原】移动web资源整理

    2013年初接触移动端,简单做下总结,首先了解下移动web带来的问题 设备更新换代快——低端机遗留下问题.高端机带来新挑战 浏览器厂商不统一——兼容问题多 网络更复杂——弱网络,页面打开慢 低端机性能 ...

  10. L2-011. 玩转二叉树

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA28AAAHQCAIAAAC5rsUiAAAgAElEQVR4nO3dzYts953n+foXcpUL0Q