本文主要包括以下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. ubuntu安装spark

    1.先得准备环境,需要JAVA环境,还有Python环境(默认会有) JAVA下载JDK之后配置JAVA环境变量 export JAVA_HOME=/opt/jdk1..0_45 export JRE ...

  2. UVA1025---A Spy in the Metro(DP)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 Secret agent Maria was sent to Alg ...

  3. XUnit学习

    1.建立测试单元项目 2.引用XUnit.dll或者在Nuget里安装XUnit 3.安装Nuget->xUnit.net[Runner: Visual Studio] 4.打开 测试-> ...

  4. 初学Hibernate之Query扩展

    1.hql参数化查询,不明确值类型的用setParameter方法:明确查询结果为一条记录的用uniqueResult方法查询 注意,参数化查询中方法setString 或 setParameter如 ...

  5. Chord算法

    转自:http://blog.csdn.net/wangxiaoqin00007/article/details/7374833 虽然网上搜索CHord,一搜一大堆,但大多讲得不太清楚明白.今天发现一 ...

  6. pom.xml

    使用intelJ idea 导入maven包管理文件是,使用Import的方式导入,会自动导入pom.xml来导入包. pom.xml会指定父子关系. 例如,总模块的pom.xml中有一下内容: &l ...

  7. DataGridView设置不自动显示数据库中未绑定的列

    项目中将从数据库查出来的数据绑定到DataGridView,但是不想显示所有的字段.此功能可以通过sql语句控制查出来的字段数目,但是DataGridView有属性可以控制不显示未绑定的数据,从UI层 ...

  8. ajax局部更新

    js //点击启用 $(".status").on("click",function(){ var id = $(this).attr("status ...

  9. WPF 窗口自定义拉伸

    .NET技术交流群 199281001 .欢迎加入. //自定义窗体拉伸 public HwndSource _HwndkaifaSource; private const int WM_SYSCOM ...

  10. 回家前的挣扎——SQLite增删改查

    引言 最后一天,公司就两个人,也不知道弄点什么,就在网上找了Sqlite的文档,看了看,这里也是现学现卖,给自己找点事做,感觉时间过得还是比较快的,不然焦急等待,滋味不好受啊. SQLite简介 SQ ...