1. 布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:fitsSystemWindows="false"
android:orientation="vertical"> <LinearLayout
android:id="@+id/index"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="vertical"> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_47"
android:layout_weight="0"
android:background="@color/white"
android:divider="@color/touming"
android:gravity="bottom"
android:orientation="horizontal"
android:padding="@dimen/dp_4"
android:showDividers="none" /> </LinearLayout>
</android.support.v4.app.FragmentTabHost>

查看布局文件

2. Activity文件代码:

public class MainActivity extends AppCompatActivity {
protected final String TAG = this.getClass().getName(); // 公用TAG
private final Class[] cls = {FirstFragment.class, OtherFragment.class};
private final String title[] = new String[]{ "首页" ,"其他" };
private final int images[] = new int[]{ R.drawable.selector_tab_shouye, R.drawable.selector_tab_other};
private FragmentTabHost mTabHost; // FragmentTabHost 对象
private LayoutInflater mInflater; // 布局转换器 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); init();
} /**
* 全局初始化
*/
private void init() {
initConfig();
initUI();
} /**
* 初始化配置
*/
private void initConfig(){
mInflater = LayoutInflater.from(this);
} /**
* 初始化界面UI 控件
*/
private void initUI() {
initTabHost();
addTab();
} /**
* 初始化 mTabHost
*/
private void initTabHost() {
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
} /**
* 添加tab
*/
private void addTab() {
for (int index = 0; index < cls.length; index ++) {
View indicatorView = mInflater.inflate(R.layout.item_shouye_indicator, null);
ImageView imageView = indicatorView.findViewById(R.id.img_indicator);
imageView.setImageResource(images[index]); // 设置指示图片, 采用selector, 实现切换
TextView tv_title_indicator = indicatorView.findViewById(R.id.tv_title_indicator);
tv_title_indicator.setText(title[index]); // 设置指示title, 采用selector, 实现切花切换
mTabHost.addTab(mTabHost.newTabSpec(cls[index].getName()).setIndicator(indicatorView), cls[index], null);
}
}
}

查看实现代码

3. 指示图片selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_launcher_round" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@mipmap/ic_launcher" android:state_selected="true"/>
<item android:drawable="@mipmap/ic_launcher" android:state_focused="true"/>
<item android:drawable="@mipmap/ic_launcher" android:state_pressed="true"/>
</selector>

selector_tab_index

4. 指示title selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/gray" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:color="@color/theme" android:state_selected="true"/>
<item android:color="@color/theme" android:state_focused="true"/>
<item android:color="@color/theme" android:state_pressed="true"/>
</selector>

selector_tab_color

快速搭建tab的更多相关文章

  1. Jenkins+Maven+SVN快速搭建持续集成环境(转)

    Jenkins是一个可扩展的持续集成引擎,Jenkins非常易于安装和配置,简单易用,下面看看我们是如何几分钟就快速搭建一个持续集成环境吧. 假设我们目前已经有2个maven项目:entities(J ...

  2. Ligerui首页的快速搭建

    一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证码的实现(经常用,记录备用) 3.Ligerui首页的快速搭建 4.Ligerui Grid组件--学生信 ...

  3. Linux快速搭建C/C++开发环境

    导读:越来越多的程序员在Linux下进行C/C++的开发.本文以CentOS 7为例,教你快速搭建一个vi + gcc/g++ + Make + valgrind的开发环境. 本文字数:1500,阅读 ...

  4. Nginx学习笔记--001-Nginx快速搭建

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...

  5. Github pages + jekyll 博客快速搭建

    Github pages + jekyll 博客快速搭建 寻找喜欢的模版 https://github.com/jekyll/jekyll/wiki/sites http://jekyllthemes ...

  6. NodeJS 最快速搭建一个HttpServer

    最快速搭建一个HttpServer 在目录里放一个index.html cd D:\Web\InternalWeb start http-server -i -p 8081

  7. 利用yeoman快速搭建React+webpack+es6脚手架

    自从前后端开始分离之后,前端项目工程化也显得越来越重要了,之前写过一篇搭建基于Angular+Requirejs+Grunt的前端项目教程,有兴趣的可以点这里去看 但是有些项目可以使用这种方式,但有些 ...

  8. 基于Docker快速搭建多节点Hadoop集群--已验证

    Docker最核心的特性之一,就是能够将任何应用包括Hadoop打包到Docker镜像中.这篇教程介绍了利用Docker在单机上快速搭建多节点 Hadoop集群的详细步骤.作者在发现目前的Hadoop ...

  9. 基于 Jenkins 快速搭建持续集成环境

      什么是持续集成 随着软件开发复杂度的不断提高,团队开发成员间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.尤其是近些年来,敏捷(Agile) 在软件工程领域越来越红火 ...

随机推荐

  1. java面试题(转)

    1.面向对象的特征有哪些方面?答:面向对象的特征主要有以下几个方面:- 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注这些 ...

  2. (蓝桥)2017C/C++A组第一题迷宫

    #include<iostream> #include<memory.h> using namespace std; char mi[10][10] ; int visited ...

  3. clock()函数的使用

    **clock()捕捉从程序开始运行到clock()被调用时所耗费的时间,这个时间单位是clock tick, 即"时钟打点." 常数CLK_TCK:机器时钟每秒所走的时钟打点数* ...

  4. 曾经遇过的sql问题

    曾经遇过的sql问题 问题一: 语句1: select SUM(level) from Comment 语句2: ELSE SUM(level) END as totalLevel from Comm ...

  5. JavaScript基本知识----操作符,流程控制,循环,字符串方法,数组方式,补充方法

    操作符 算术运算 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. 【微软2017年预科生计划在线编程笔试 A】Legendary Items

    [题目链接]:https://hihocoder.com/problemset/problem/1489 [题意] 每轮游戏; 你一开始有p的概率获得超神标记; 如果这轮游戏你没获得超神标记; 那么你 ...

  7. 洛谷——P2925 [USACO08DEC]干草出售Hay For Sale

    https://www.luogu.org/problem/show?pid=2925 题目描述 Farmer John suffered a terrible loss when giant Aus ...

  8. js移除style属性

    这个属性是通过   div.style.color="red"   这种类似添加的,想要添加重置函数,使用div.removeAttribute("style" ...

  9. cat<<EOF获取标准输入到文件中

    原文:http://blog.csdn.net/apache0554/article/details/45508631 ---------------------------------------- ...

  10. Eclipse启动时提示fail to create the Java Virtual Machine问题的解决

    今天偶然打开Eclipse.发现无法打开,出现例如以下提示: 后来经过上网查询.发现是eclipse.ini文件的问题,打开eclipse安装文件夹下的eclipse.ini文件: -startup ...