本文主要包括以下Tab类实现方式

  1. FragmentTabHost+Fragment实现
  2. 传统的ViewPager实现
  3. FragmentManager+Fragment实现
  4. ViewPager+FragmentPagerAdapter实现
  5. TabPageIndicator+ViewPager+FragmentPagerAdapter

FragmentTabHost+Fragment实现

布局文件

<?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="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout> <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="60dip" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost> </LinearLayout>

MainActivity

package com.darna.wmxfx;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView; import com.darna.wmxfx.fragment.Frg_Cart;
import com.darna.wmxfx.fragment.Frg_Index;
import com.darna.wmxfx.fragment.Frg_UserCenter; public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost = null;
private int iTabIndex = 0;
private int[][] iTab = new int[][] { { R.drawable.indexunused, R.drawable.cartunused, R.drawable.usercenterunused },
{ R.drawable.indexused, R.drawable.cartused, R.drawable.usercenterused } }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
} private void initViews(){
String[] strTab = getResources().getStringArray(R.array.bottom_tab);
RelativeLayout[] relativeLayout = new RelativeLayout[strTab.length];
for(int i = 0; i < strTab.length; i++){
if (i == 0) {
relativeLayout[i] = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_bottom, null);
relativeLayout[i].findViewById(R.id.iv_bottom_tab).setBackgroundResource(iTab[1][i]);
TextView tView = (TextView) relativeLayout[i].findViewById(R.id.tv_bottom_tab);
tView.setText(strTab[i]);
tView.setTextColor(getResources().getColor(R.color.textused));
}else {
relativeLayout[i] = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_bottom, null);
relativeLayout[i].findViewById(R.id.iv_bottom_tab).setBackgroundResource(iTab[0][i]);
TextView t = (TextView) relativeLayout[i].findViewById(R.id.tv_bottom_tab);
t.setText(strTab[i]);
t.setTextColor(getResources().getColor(R.color.textunused));
}
} mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_INDEX)
.setIndicator(relativeLayout[0]), Frg_Index.class, null); mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_CART)
.setIndicator(relativeLayout[1]), Frg_Cart.class, null); mTabHost.addTab(mTabHost.newTabSpec(Config.KEY_USERCENTER)
.setIndicator(relativeLayout[2]), Frg_UserCenter.class, null); mTabHost.setOnTabChangedListener(new OnTabChangeListener() { @Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
View v = mTabHost.getTabWidget().getChildAt(iTabIndex);
((ImageView) v.findViewById(R.id.iv_bottom_tab)).setBackgroundResource(iTab[0][iTabIndex]);
((TextView) v.findViewById(R.id.tv_bottom_tab)).setTextColor(getResources().getColor(R.color.textunused));
v = mTabHost.getCurrentTabView();
iTabIndex = mTabHost.getCurrentTab();
((ImageView) v.findViewById(R.id.iv_bottom_tab)).setBackgroundResource(iTab[1][iTabIndex]);
((TextView) v.findViewById(R.id.tv_bottom_tab)).setTextColor(getResources().getColor(R.color.textused));
}
}); }
}

效果如下

传统的ViewPager实现

FragmentManager+Fragment实现

ViewPager+Fragment实现

TabPageIndicator+ViewPager+FragmentPagerAdapter

实现方式和3是一致的,但是使用了TabPageIndicator作为tab的指示器,效果还是不错的,这个之前写过,就不再贴代码了。

效果图:

后四种方式详细可以参见

Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager - Hongyang - 博客频道 - CSDN.NET

完成

Android之Tab类总结的更多相关文章

  1. (转) Android的Window类

    Android的Window类 2011-03-25 10:02 by Keis, 110 visits, 网摘, 收藏, 编辑 Android的Window类(一)  Android的GUI层并不复 ...

  2. Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法

    Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法 如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1i ...

  3. 53. Android常用工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...

  4. Android 常见工具类封装

    1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...

  5. android.hardware.Camera类及其标准接口介绍

    android.hardware.Camera类及其标准接口介绍,API level 19 http://developer.android.com/reference/android/hardwar ...

  6. android中Handle类的用法

    android中Handle类的用法 当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无 ...

  7. 从源码角度理解android动画Interpolator类的使用

    做过android动画的人对Interpolator应该不会陌生,这个类主要是用来控制android动画的执行速率,一般情况下,如果我们不设置,动画都不是匀速执行的,系统默认是先加速后减速这样一种动画 ...

  8. Android中Cursor类的概念和用法

    http://blog.sina.com.cn/s/blog_618199e60101fskp.html 使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android ...

  9. android 文件操作类简易总结

    android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...

随机推荐

  1. UvaLive 5026 Building Roads

    传送门 Time Limit: 3000MS Description There is a magic planet in the space. There is a magical country ...

  2. Aop 是面向切面编程,

    Aop 是面向切面编程,是在业务代码中可以织入其他公共代码(性能监控等),现在用普通的方法实现AOP http://blog.csdn.net/heyanfeng22/article/details/ ...

  3. 多线程下载的原理&断点下载的原理

    1)多线程下载说明:

  4. while练习:输入一个班级的人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩。

    Console.WriteLine("请输入班级的总人数:"); int count = int.Parse(Console.ReadLine()); ;//声明一个循环变量来记录 ...

  5. 使用prompt输入一句英文句子和排序方式(升/降),将所有单词按排序方式排序后在网页上输出

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. java分布式通信系统(J2EE分布式服务器架构)

    一.序言 近几个月一直从事一个分布式异步通信系统,今天就整理并blog一下. 这是一个全国性的通信平台,对性能,海量数据,容错性以及扩展性有非常高的要求,所以在系统的架构上就不能简单的采用集中式.简单 ...

  7. html5摇一摇[转]

    写在前面 年底了,有些公司会出一个摇奖的活动,今天在家没事就搜了一下这方面的资料. 原文地址:http://www.cnblogs.com/waitingbar/p/4682215.html 测试 效 ...

  8. Math.Round函数详解

    有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...

  9. NGUI 学习笔记实战之二——商城数据绑定(Ndata)

    上次笔记实现了游戏商城的UI界面,没有实现动态数据绑定,所以是远远不够的.今天采用NData来做一个商城. 如果你之前没看过,可以参考上一篇博客   NGUI 学习笔记实战——制作商城UI界面  ht ...

  10. Heap(堆)和stack(栈)有的区别是什么。

    java的内存分为两类,一类是栈内存,一类是堆内存.栈内存是指程序进入一个方法时,会为这个方法单独分配一块私属存储空间,用于存储这个方法内部的局部变量,当这个方法结束时,分配给这个方法的栈会释放,这个 ...