Android之Tab类总结
本文主要包括以下Tab类实现方式
- FragmentTabHost+Fragment实现
- 传统的ViewPager实现
- FragmentManager+Fragment实现
- ViewPager+FragmentPagerAdapter实现
- 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类总结的更多相关文章
- (转) Android的Window类
Android的Window类 2011-03-25 10:02 by Keis, 110 visits, 网摘, 收藏, 编辑 Android的Window类(一) Android的GUI层并不复 ...
- Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法
Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法 如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1i ...
- 53. Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- Android 常见工具类封装
1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...
- android.hardware.Camera类及其标准接口介绍
android.hardware.Camera类及其标准接口介绍,API level 19 http://developer.android.com/reference/android/hardwar ...
- android中Handle类的用法
android中Handle类的用法 当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无 ...
- 从源码角度理解android动画Interpolator类的使用
做过android动画的人对Interpolator应该不会陌生,这个类主要是用来控制android动画的执行速率,一般情况下,如果我们不设置,动画都不是匀速执行的,系统默认是先加速后减速这样一种动画 ...
- Android中Cursor类的概念和用法
http://blog.sina.com.cn/s/blog_618199e60101fskp.html 使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android ...
- android 文件操作类简易总结
android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...
随机推荐
- hihocoder 1169 猜数字
传送门 时间限制:10000ms 单点时限:5000ms 内存限制:256MB 描述 你正在和小冰玩一个猜数字的游戏.小冰首先生成一个长为N的整数序列A1, A2, …, AN.在每一轮游戏中,小冰会 ...
- appium向右滑动
/*** * 右滑1/2屏幕 / public static void slideRight(){ int x=driver.manage().window().getSize().width; in ...
- CATransform3D
本章介绍图层的几何组成部分,及他们之间的相互关,同时介绍如何变换矩阵可以产生复杂的视觉效果. 1.1 图层的坐标系 图层的坐标系在不同平台上面具有差异性.在iOS系统中,默认的坐标系统原点在图层的中心 ...
- MyEclipse------如何连接MySQL
testconnection.jsp <%@ page language="java" import="java.util.*" pageEncoding ...
- 用多态来实现U盘,Mp3,移动硬盘和电脑的对接,读取写入数据。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 初学Struts2-自定义拦截器及其配置
自定义拦截器,首先新建一个继承自AbstractInterceptor类的类,然后重写intercept方法,代码如下 public class HelloInterceptor extends Ab ...
- JavaScript的apply和call方法及其区别
参考资料: http://blog.csdn.net/myhahaxiao/article/details/6952321 apply和call能“劫持”其他对象的方法来执行,其形参如下: apply ...
- SSH协议及其应用
SSH协议及其应用 原文作者:阮一峰 链接: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruany ...
- php排序 sort、rsort、asort、arsort、ksort、krsort
sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort() 函数用于对数组单元从低到高进行排序并保持索引关系. arsort() 函数用于 ...
- C++中虚函数的作用浅析
虚函数联系到多态,多态联系到继承.所以本文中都是在继承层次上做文章.没了继承,什么都没得谈. 下面是对C++的虚函数这玩意儿的理解. 一, 什么是虚函数(如果不知道虚函数为何物,但有急切的想知道,那你 ...