由于业务需要,需要在使用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. iosselect:一个js picker项目,在H5中实现IOS的下拉效果

    iosselect是在webapp下的一个picker组件,可以轻松实现各类选择器效果.比如地区选择 时间选择 日期选择等. 下面是一个地址选择器demo截图,可以访问:http://zhoushen ...

  2. 【初探IONIC】不会Native可不可以开发APP?

    前言 Hybrid技术流行已经有一段日子了,楼主的关注点也一直围绕着移动端围绕着Hybrid相关展开,Hybrid已经是大大提升开发效率的开发方式了,但是仍然需要至少一个IOS与Andriod,那么可 ...

  3. SQLSERVER基础语句(一)

    1.插入一行数据:INSERT INTO 表名(列名)VALUES(对应的值);2.一次性插入多条数据先建表:INSERT INTO 新建表名(列表)SELECT 原始表列名 FROM 原始表:执行时 ...

  4. iOS 下拉刷新-上拉加载原理

    前言 讲下拉刷新及上拉加载之前先给大家解释UIScrollView的几个属性 contentSize是UIScrollView可以滚动的区域. contentOfinset 苹果官方文档的解释是&qu ...

  5. directx12中vetex buffer、index buffer和constant buffer绑定piple line的时机

    类别 时机 函数 建Heap vetex buffer 在Draw函数中 ID3D12GraphicsCommandList::IASetVertexBuffer 否 index buffer 在Dr ...

  6. "Becoming Functional" 阅读笔记+思维导图

    <Becoming Functional>是O'Reilly公司今年(2014)7月发布的一本薄薄的小册子,151页,介绍了函数式编程的基本概念.全书使用代码范例都是基于JVM的编程语言, ...

  7. Centos 6.5 Percona 5.6.27 Tokudb 配置

    参考 https://www.percona.com/doc/percona-server/5.6/tokudb/tokudb_installation.html # wget https://www ...

  8. monkeyrunner之控件ID不存在或重复

    我们在用monkeyrunner进行Android自动化时,通过获取坐标点或控件ID进行一系列操作.由于使用坐标点时,屏幕分辨率一旦更改,则代码中用到坐标的地方都要修改,这样导致代码的复用率较低.因此 ...

  9. 理解Docker(6):若干企业生产环境中的容器网络方案

    本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  10. POJ 2398 Toy Storage(计算几何)

    题意:给定一个如上的长方形箱子,中间有n条线段,将其分为n+1个区域,给定m个玩具的坐标,统计每个区域中的玩具个数. 题解:通过斜率判断一个点是否在两条线段之间. /** 通过斜率比较点是否在两线段之 ...