开通博客以来已经约莫1个月了。几次想提笔写写东西,但总是由于各种各样的原因并没有开始。现在,年假刚结束,项目也还没有开始,但最终促使我写这篇博客的是,看了一篇博友写的新年计划,说是要在新的一年中写50篇博客,我也心血来潮的定下了这样的目标。把年前项目中用到的FragmentTabHost在这里总结一下。

  现在市面上app的主流框架大体分为两种:一种是在主界面点击菜单按钮,之后会滑出侧滑菜单,之后进入到各个模块,还有一种是在主界面的下面放置若干个tab按钮,点击按钮,切换到不同的模块。今天要讲的就是第二种的实现方式之一的FragmentTabHost.

  FragmentTabHost来自于android.support.v4.app这个包下,继承自TabHost,作为android4.0的控件。好了,废话还是少说为妙。

下面是主界面的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.itrui.searchs.MainActivity" > <android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#D0D0D0">
</TextView>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FFFFFF"
android:layout_gravity="bottom"
android:padding="10dp"
></TabWidget>
</LinearLayout>
</android.support.v4.app.FragmentTabHost> </RelativeLayout>

控件的命名是固定的不能随便更改。控件的id必须是Android提供的标准id, 即"@android:id"

Tab的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/layout_ancor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="vertical"
android:padding="2dp"
android:paddingBottom="15dp"
android:paddingTop="10dp">
<ImageView
android:id="@+id/img_tab_pic"
android:layout_width="32dp"
android:layout_height="32dp"
/>
</LinearLayout> </RelativeLayout>

此布局文件是一张图片,当然你也可以根据自己的需求来添加其他的控件,比如文字之类的控件

其中之一的Fragment的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/fragment_main_frist_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:layout_marginLeft="80dp"
android:text="poi搜索"/>
<ImageView
android:id="@+id/iv_baidu_dingwei"
android:layout_width="32dp"
android:layout_height="32dp"
android:paddingTop="10dp"
android:paddingRight="5dp"
android:layout_alignParentRight="true"
android:src="@drawable/baidumap"/>
</RelativeLayout>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是主界面的第一个fragment"/> </LinearLayout>

当然你也可以同一个fragment实现复用。

主页面的代码:

public class MainActivity extends FragmentActivity {

    private FragmentTabHost myTabhost;
//Tab图片
private int mImages[] = {
R.drawable.tab_assistant_gray,R.drawable.tab_center_gray,R.drawable.tab_contest_gray,R.drawable.tab_counter_gray
};
//标记
private String mFragmentTags[] ={
"第一个","第二个","第三个","第四个"
};
//加载的Fragment
private Class mFragment[] ={ MainFristFragment.class,MainSecondFragment.class,MainThridFragment.class,MainFristFragment.class };
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTabHost();
}
private void initTabHost() {
myTabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);
myTabhost.setup(this, getSupportFragmentManager(),android.R.id.tabhost);
//去掉分割线
myTabhost.getTabWidget().setDividerDrawable(null);
for(int i = 0;i<mImages.length;i++){
//对Tab按钮添加标记和图片
TabSpec tabSpec = myTabhost.newTabSpec(mFragmentTags[i]).setIndicator(getImageView(i));
//添加Fragment
myTabhost.addTab(tabSpec,mFragment[i],null);
myTabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.white); } } //获取图片资源
private View getImageView(int index){
View view = getLayoutInflater().inflate(R.layout.tab_title, null);
ImageView imageView = (ImageView) view.findViewById(R.id.img_tab_pic);
imageView.setImageResource(mImages[index]);
return view; } }

在onCreat()中执行 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);语句的作用是让手机屏幕保持一种不暗不关闭的效果。通常的应用场景是视频播放器。
总结:

在FragmentTabHost这一个布局中包裹着FrameLayout(也可以换成其他布局主要作用是存放fragment)和TabWidget控件。

执行语句:

myTabhost.setup(this, getSupportFragmentManager(),android.R.id.tabhost);

去掉分割线

myTabhost.getTabWidget().setDividerDrawable(null);

向FragmentTabHost中添加标识和添加图标

TabSpec tabSpec = myTabhost.newTabSpec(mFragmentTags[i]).setIndicator(getImageView(i));

将对应的fragment添加到控件中去

myTabhost.addTab(tabSpec,mFragment[i],null);

其实还是蛮好用的一个控件。

  

FragmentTabHost的基本用法的更多相关文章

  1. FragmentTabHost用法

    FragmentTabHost组成 Tabhost,TabWidget,切换的内容容器FrameLayout 层级关系 ----FragmentTabHost |-----TabWidget |--- ...

  2. 【Android Widget】FragmentTabHost

    android.support.v4包里面提供了FragmentTabHost用来替代TabHost,FragmentTabHost内容页面支持Fragment,下面我们就通过示例来看他的用法 效果图 ...

  3. FragmentTabHost的应用

    原创)FragmentTabHost的应用(fragment学习系列文章之二) 时间 2014-04-14 00:11:46  CSDN博客 原文  http://blog.csdn.net/flyi ...

  4. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  5. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  6. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  7. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  8. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  9. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

随机推荐

  1. MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信

    MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...

  2. 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)

    前言 Entity Framework 延伸系列目录 今天来说说EF与MVC项目的性能检测和监控 首先,先介绍一下今天我们使用的工具吧. MiniProfiler~ 这个东西的介绍如下: MVC Mi ...

  3. Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新

    因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...

  4. 计算机程序的思维逻辑 (54) - 剖析Collections - 设计模式

    上节我们提到,类Collections中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了第一类,本节我们介绍第二类. 第二类方法大概可以分为两组: 接受其他 ...

  5. java设计模式之单例模式(几种写法及比较)

    概念: Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建 ...

  6. arcpy+PyQt+py2exe快速开发桌面端ArcGIS应用程序

    前段时间有一个项目,大体是要做一个GIS数据处理工具. 一般的方法是基于ArcObjects来进行开发,因为我对ArcObjects不太熟悉,所以就思考有没有其他简单快速的方法来做. 在查看ArcGI ...

  7. Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录

    一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...

  8. 搭建个人wordpress博客(小白教程)

    新浪sae平台现在是有个免费个人空间使用,现在,教您如何使用该平台搭建属于自己的个人网站,本教程以wordpress程序安装包搭建个人网站. 申请新浪云账号 如果我们使用SAE新浪云计算平台作为服务器 ...

  9. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  10. innodb 自增列重复值问题

    1 innodb 自增列出现重复值的问题 先从问题入手,重现下这个bug use test; drop table t1; create table t1(id int auto_increment, ...