在上一篇博客《TabHost选项卡的实现(一):使用TabActivity实现》中,讲解了如何使用TabActivity创建管理选项卡,但是,通过TabActivity创建选项卡的方式已经不再推荐使用,Android3.0之后推出Fragment,并推荐我们使用Fragment来实现标签页。关于Fragment的使用,可以参考我之前的几篇博文:

1. Fragment学习(一) :生命周期

2. Fragment开发实战(一)

3. Fragment学习(二): 管理Fragment和Fragment通讯

4. Fragment开发实战(二)

如果已经对Fragment很了解了,那接下来,我们介绍,如何使用Fragment来实现TabHost,效果图如下:

开发过程:

首先,我们需要定义一个Activity,该Activity管理了社会新闻、生活新闻、娱乐新闻、军事新闻这四个子布局,也就是Fragment。我们先定义该Activity的布局界面:

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:id="@+id/tb1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="社会新闻" /> <TextView
android:id="@+id/tb2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="生活新闻" /> <TextView
android:id="@+id/tb3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="娱乐新闻" /> <TextView
android:id="@+id/tb4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="军事新闻" />
</LinearLayout> <LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout> </LinearLayout>

我们可以看出,程序使用四个TextView代表了效果图上的四个Tab标签页,在实际的开发中,我们可以为该标签做一些效果,使其更加美观,此处介绍不再详细介绍。

在页面布局的下面,我们定义了一个id为content的LinearLayout布局,该布局负责动态替换Fragment的布局。

接下来,我们定义四个Fragment,每个Fragment管理一个子布局,因为Demo里每个Fragment都相似,此处只贴出一个Fragment1.java的代码:

package com.chen.yuan.fragment;

import com.chen.yuan.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment1 extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment1, null);
}
}

该Fragment管理的布局文件为fragment1.xml,我们根据需要定义布局内容:

<?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"
> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="社会新闻"
android:textColor="#ff00ff"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
/> </LinearLayout>

四个标签页,对应于四个Fragment,而Fragment应该归于Activity管理,我们使用Fragment动态的管理Fragment:

public class MainActivity extends Activity implements OnClickListener
{
private TextView tv1 = null; private TextView tv2 = null; private TextView tv3 = null; private TextView tv4 = null; private FragmentManager fm = null; private FragmentTransaction ft = null; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.tb1);
tv2 = (TextView) findViewById(R.id.tb2);
tv3 = (TextView) findViewById(R.id.tb3);
tv4 = (TextView) findViewById(R.id.tb4); tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
tv4.setOnClickListener(this); fm = getFragmentManager();
ft = fm.beginTransaction(); ft.replace(R.id.content, new Fragment1());
ft.commit();
} @Override
public void onClick(View v)
{
ft = fm.beginTransaction();
switch (v.getId())
{
case R.id.tb1: ft.replace(R.id.content, new Fragment1()); break;
case R.id.tb2:
ft.replace(R.id.content, new Fragment2());
break;
case R.id.tb3:
ft.replace(R.id.content, new Fragment3());
break;
case R.id.tb4:
ft.replace(R.id.content, new Fragment4());
break; default:
break;
}
ft.commit();
}
}

代码具体啥意思,我就不再说明了,建议先学会Fragment的基本使用再看该例子,很简单。

代码下载地址免费: http://download.csdn.net/detail/zuiwuyuan/7986609

TabHost选项卡的实现(二):使用Fragment实现的更多相关文章

  1. 【Android 应用开发】Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  2. Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  3. Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡

     <Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePa ...

  4. Android系列之Fragment(二)----Fragment的生命周期和返回栈

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  5. 【转】【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法

    原始地址:http://blog.csdn.net/yangyu20121224/article/category/1431917/1 由于TabActivity在Android4.0以后已经被完全弃 ...

  6. fragment做成选项卡,tab效果。 fragment+RadioGroup

    fragment做成选项卡,tab效果. fragment+RadioGroup from://http://blog.csdn.net/zimo2013/article/details/122393 ...

  7. TabHost选项卡的实现(一):使用TabActivity实现

    一. TabHost的基本开发流程 TabHost是一种非常实用的组件,可以很方便的在窗口上防止多个标签页,每个标签页相当于获得了一个外部容器相同大小的组件摆放区域. 我们熟悉的手机电话系统" ...

  8. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...

  9. android学习--TabHost选项卡组件

    TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...

随机推荐

  1. JZOJ 平衡的子集

    Description 夏令营有N个人,每个人的力气为M(i).请大家从这N个人中选出若干人,如果这些人可以分成两组且两组力气之和完全相等,则称为一个合法的选法,问有多少种合法的选法? Input 第 ...

  2. MR25H10-1Mb密度SPI串行接口MRAM

    everspin的MR25H10是一个1,048,576位磁阻随机存取存储器(MRAM)设备,由131,072个8位字组成.MR25H10提供串行EEPROM和串行闪存兼容的读/写时序,没有写延迟,并 ...

  3. Docker.[2].安装Docker.

    Docker.[2].安装Docker. 学习Docker的时候,也是搜索了好多资料,捣鼓那些理论的东西.什么是镜像?什么是容器 ?特点?用途?.... 巴拉巴拉一大堆. 我这里不再讲述理论,只记录代 ...

  4. SDUT-3402_数据结构实验之排序五:归并求逆序数

    数据结构实验之排序五:归并求逆序数 Time Limit: 50 ms Memory Limit: 65536 KiB Problem Description 对于数列a1,a2,a3-中的任意两个数 ...

  5. golang之下载安装配置

    1.下载:根据操作系统和计算架构选择合适的安装包,操作系统类型有linux.mac.windows等,计算架构分为32位的386计算架构和64位的amd64计算架构 2.安装:推荐安装到 /usr/l ...

  6. Bnd教程(1):如何用命令行安装bnd

    1. 如果你用的是MacOS,请运行: brew install bnd 然后运行bnd version看是否安装成功: $ bnd version 4.0.0.201801191620-SNAPSH ...

  7. Linux常用命令2 权限管理命令

    1.权限管理命令:chmod 上面图片中的ugoa与rwx并不是一个命令,而是不同选项 u 所有者  g 所属组 o 其他人 a 所有人 r 读取权限 w写入权限  x 执行权限 chmod u+x ...

  8. sas教程

    http://web5.pku.edu.cn/pucssr/SASbiancheng.pdf 本教程中的主题将向您介绍 SAS Enterprise Guide.您最好依次浏览这些主题. 概述 启动项 ...

  9. li设置多选和取消选择的样式、输入数据类型判断

    li设置多选和取消选择的样式: $('li').click(function(){ if($(this).hasClass('active')) {$(this).removeClass('activ ...

  10. github中markdown语言的使用规则

    开始使用github就接触了markdown,确实如它的宗旨所言"易读易写",语法简洁明了,功能比纯文本更强,是一种非常适用于网络的书写语言.并且一大优点是兼容HTML,只要不在m ...