fragmentTabHost 使用示例
目前我们看微信的底部,有四个导航栏,那我们应该来怎么实现类似的导航栏呢?

在 android 4.0 的时候,推出了一个新的工具,fragmentTabHost 。
fragmentTabHost 可以自己自定义底部的样式,你可以自由添加图标或者文字,都可以。那我们怎么来使用呢?
首先我们来看 MainActivity;
public class MainActivity extends AppCompatActivity {
private FragmentTabHost fragmentTabHost;
private String texts[] = { "首页", "通讯录", "发现", "我", "更多" };
private int imageButton[] = { R.drawable.bt_home_selector,
R.drawable.bt_home_selector, R.drawable.bt_home_selector,R.drawable.bt_home_selector ,R.drawable.bt_home_selector};
private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化tabhost
fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(),
R.id.maincontent);
for (int i = 0; i < texts.length; i++) {
TabHost.TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
fragmentTabHost.addTab(spec, fragmentArray[i], null);
//设置背景(必须在addTab之后,由于需要子节点(底部菜单按钮)否则会出现空指针异常)
fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bt_selector);
}
}
private View getView(int i) {
//取得布局实例
View view=View.inflate(MainActivity.this, R.layout.tab_item_view, null);
//取得布局对象
ImageView imageView=(ImageView) view.findViewById(R.id.image);
TextView textView=(TextView) view.findViewById(R.id.text);
//设置图标
imageView.setImageResource(imageButton[i]);
//设置标题
textView.setText(texts[i]);
return view;
}
}
在 fragmentTabHost.setup 函数中,我们需要传入三个参数,一个是上下文内容Context,一个是FragmentManager,第三个是放置fragment的容器的id;
注意容器必须是 FrameLayout 类型,因为内部定义的类型就是这个。内部会根据id来进行初始化。
fragmentTabHost.newTabSpec(texts[i]) 是定义 tag 的,方便后续根据 tag 来查找具体的 fragment。tag 就是 texts[i] 中的值;
setIndicator(getView(i)) 是为每一个 fragment 指定一个 view 指示器,这个 view 我们可以自己定义,具体引用就是 getView 函数。
addtab 函数则将 fragment 和 tag 联系在一起。
在 getView 中,我们可以自己自定义一个 itemView 来作为 tab 的样式,这个就不具体展开了,看看函数就懂。
然后我们再来简单看看 fragment 怎么写:
public class FragmentPage1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_1, null);
}
}
接下去我们看下具体的布局文件:
首先是整体的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <!-- 存放主要页面内容 --> <FrameLayout
android:id="@+id/maincontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout> <!-- 底层菜单 --> <android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" >
</FrameLayout>
</android.support.v4.app.FragmentTabHost> </LinearLayout>
接下去是 tab 的itemview 的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView
android:id="@+id/text"
android:padding="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
/> </LinearLayout>
最后就是fragment 的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我是第一个Fragment"
android:textSize="20dp"
/> </RelativeLayout>
好了,到这里,我们就基本讲完了。
附上源码:https://github.com/huanshen/Learn-Android/tree/master/fragmentTabHost
fragmentTabHost 使用示例的更多相关文章
- Tablayout ViewPage 使用示例
上一篇文章介绍了使用 FragmenttabHost 来使用 tab 导航:到 Android 5.0 的时候,又推出了 TabLayout.因此,有必要对tablayout 进行了解下. 首先我们来 ...
- 【Android Widget】FragmentTabHost
android.support.v4包里面提供了FragmentTabHost用来替代TabHost,FragmentTabHost内容页面支持Fragment,下面我们就通过示例来看他的用法 效果图 ...
- Android系统示例之ActionBarCompat
导入工程ActionBarCompat时,出现错误.从其他工程下拷贝project.propertiest文件过来,问题仍在.拷贝后需要重启Eclipse才解决.问题如下: [2013-07-03 1 ...
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
- FragmentTabHost的基本用法
开通博客以来已经约莫1个月了.几次想提笔写写东西,但总是由于各种各样的原因并没有开始.现在,年假刚结束,项目也还没有开始,但最终促使我写这篇博客的是,看了一篇博友写的新年计划,说是要在新的一年中写50 ...
- JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
随机推荐
- 演示 Calendar 的一般操作
package com.yixin.webbrower; /* * 演示 Calendar 的一般操作 */ import java.util.Date; import java.text.Simpl ...
- Scala类型系统(sudden thought)
http://docs.scala-lang.org/tour/lower-type-bounds.html中有一段代码 trait Node[+B] { def prepend(elem: B): ...
- 吐槽CSDN--想钱想疯了--阅读全文需要关闭广告屏蔽
吐槽CSDN 想钱想疯了–阅读全文需要关闭广告屏蔽 近来csdn开始主推博客皮肤升级,说白了就是有一套新的盈利模式,具体怎么操作呢: 1. 采用信息流方式,博客内容变成类似朋友圈.微博.知乎那样的信息 ...
- 一份传世典文:十年编程(Teach Yourself Programming in Ten Years)
原文:Teach Yourself Programming in Ten Years作者:郭晓刚翻译:郭晓刚(foosleeper@163.net)最后修订日期:2004-3-192005-01-12 ...
- 深入理解计算机系统(4.2)------逻辑设计和硬件控制语言HCL
上一篇博客我们简单介绍了Y86指令集体系,而这篇博客我们将介绍指令集体系的逻辑设计和硬件控制语言HCL,为后面去实现Y86打下基础. 在硬件设计中,用电子电路来计算对位进行运算的函数,以及在各种存储器 ...
- debian change system language
1. select locales: 2. set language: sudo localectl set-locale LANG=zh_CN.utf8 sudo localectl set-loc ...
- Java8系列之初识
前言:终于有机会在工作中使用高版本的Java8,但是一直没有对java8中添加的新特性进一步了解过,所以趁着这个机会学习一下,能够在编程中熟练的使用. 一.接口的改变 我们知道,在java8版本以前, ...
- centos安装openoffice服务
第一步:yum install openoffice.org-brand openoffice.org-core openoffice.org-java-common xvfb openoffice. ...
- Mybatis Mapper.xml 需要查询返回List<String>
当需要查询返回 List<String> <select id="getByIds" parameterType="java.lang.String&q ...
- Dagger2 入门解析
前言 在为dropwizard选择DI框架的时候考虑了很久.Guice比较成熟,Dagger2主要用于Android.虽然都是google维护的,但Dagger2远比guice更新的频率高.再一个是, ...