FragmentTabHost + Fragment 使用小记
由于业务需要,需要在使用Activity的顶部使用一个导航栏,点击导航栏的tab,下面显示内容。决定采用项目中已经使用过的FragmentTabHost + Fragment的方式实现。不同的是之前的FragmentTabHost位于底部(下面统称:Bottom),现在需要放置在顶部(下面统称:Top)。下面将对这两种方式进行比较:
Bottom:
Activity布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/activity_main" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" > 7 8 <FrameLayout 9 android:id="@+id/content_main" 10 android:layout_width="match_parent" 11 android:layout_height="0dp" 12 android:layout_weight="1" 13 android:background="@color/white"/> 14 15 <android.support.v4.app.FragmentTabHost 16 android:id="@+id/tabHost_main" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:layout_marginTop="5dp"> 20 <FrameLayout 21 android:id="@+id/tabContent" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content"/> 24 </android.support.v4.app.FragmentTabHost> 25 26 </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.FragmentTabHostandroid:id="@+id/tab_host"android:layout_width="match_parent"android:layout_height="40dp"/><FrameLayoutandroid: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"><FrameLayoutandroid:layout_marginTop="42dp"android:id="@+id/fragment_content"android:layout_width="match_parent"android:layout_height="match_parent"/><android.support.v4.app.FragmentTabHostandroid:id="@+id/tab_host"android:layout_width="match_parent"android:layout_height="40dp"/></RelativeLayout>
成功:
这里为什么使用LinlearLayout不成功,换成RelativeLayout可以,最终的原因,待研究清楚再总结。
转载于:https://www.cnblogs.com/ithaibo-sit/p/6023316.html
FragmentTabHost + Fragment 使用小记相关推荐
- 使用FragmentTabHost+Fragment+viewpager 实现滑动分页
之前分页效果一直用TabActivity+TabHost,但是android3.0后就不在推荐使用了,而是推荐使用Fragment,经过研究加参考其他朋友代码实现了滑动分页的效果,代码简单附上. 主页 ...
- 【Android】FragmentTabHost实现底部Tab菜单选项
以前实现类似微博底部菜单使用的是TabHost+Activity来实现,但是使用的时候提醒已经被弃用,现在我们可以通过FragmentTabHost+Fragment来实现.下面就是demo: 1.m ...
- FragmentTabHostUnderLineDemo【FragmentTabHost带下划线】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现顶部选项卡(带下划线效果)展现. 效果图 代码分析 1.该Demo中采用的是FragmentT ...
- FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity
自Android3.2之后,TabActibvity被弃用(Deprecated).取而代之的是FragmentActivity.由于Fragment比Activiy更灵活.消耗的资源更小.全然可以满 ...
- Android典型界面设计(3)——访网易新闻实现双导航tab切换
一.问题描述 双导航tab切换(底部区块+区域内头部导航),实现方案底部区域使用FragmentTabHost+Fragment, 区域内头部导航使用ViewPager+Fragment,可在之前博客 ...
- Android实战:手把手实现“捧腹网”APP(三)-----UI实现,逻辑实现
APP页面实现 根据原型图,我们可以看出,UI分为两部分,底部Tab导航+上方列表显示. 所以此处,我们通过 FragmentTabHost+Fragment,来实现底部的导航页面,通过Recycle ...
- Android之Tab类总结
本文主要包括以下Tab类实现方式 FragmentTabHost+Fragment实现 传统的ViewPager实现 FragmentManager+Fragment实现 ViewPager+Frag ...
- android 底部导航总结
1BottomNavigationView实现 实现方式: 1.1 BottomNavigationView是放置在design包中的,所以,使用前需要先引入com.android.support:d ...
- Android项目实战——菜鸟商城
Android-菜鸟商城项目实战 实战B站视频的菜鸟商城项目,每个章节有对应的知识点讲解,代码中有自己的注释和理解. 视频地址:https://www.bilibili.com/video/BV1RJ ...
最新文章
- 【深度学习入门到精通系列】Mean Iou
- 第三周课程总结实验报告
- redis 槽点重新分配 集群_redis集群高可用部署-cluster-槽点的迁移查看
- prettier 配置参数说明
- Intro.js轻松搞定页面引导流程
- 利用优先级队列实现堆栈
- RMAN catalog 的创建和使用
- java实现中缀表达式转后缀表达式
- PostgreSQL 会话级资源隔离探索
- android手机api等级_Android Api级别
- linux远程控制本地用户登录,linux 本地无法登录 远程可以登陆的解决办法
- Spring源码阅读 —— 一文看懂AOP的流程
- JS回调函数、真实举例
- HBuilderX搭建Vue项目
- 《系统分析与设计》课程设计——医院门诊信息管理查询系统
- ffmpeg java 实时视频流转码
- H2O技术方案预研分析
- 表达式 625%2.5 是合法的c语言表达式,2018-C程序设计-期末自测题.doc
- 关于Python的面向对象
- 世界杯快结束了,VAR的故事才刚刚开始