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. soui edit passwrod模式下禁用输入法

    一直在用soui做客户端界面,今天发现密码edit在中文输入法下不能输入密码.我在想难道不是这样吗,密码就该用英文输入法啊. 然后我就用mfc的做了个demo,发现mfc的edit在密码模式下是可以用 ...

  2. Spring框架学习之SpringAOP(二)

    AOP概念 AOP(Aspect-Oriented Programming,面向切面编程),AOP是OOP(面向对象编程)的补充和完善 AOP的核心思想就是“将应用程序中的商业逻辑同对其提供支持的通用 ...

  3. 二叉堆 及 大根堆的python实现

    Python 二叉堆(binary heap) 二叉堆是一种特殊的堆,二叉堆是完全二叉树或者是近似完全二叉树.二叉堆满足堆特性:父节点的键值总是保持固定的序关系于任何一个子节点的键值,且每个节点的左子 ...

  4. Linux之网络ping(unknown host)故障及yum no more mirrors to try

    1.ping外网出现ping:unknown host   字样故障 鉴于网上大多都是提供临时生效的解决办法,这里不再赘述,并提供下永久生效的方案: 永久生效: 1)添加DNS地址和下一跳网关地址至网 ...

  5. Linux环境变量设置命令export(转)

    Linux export命令用于设置或显示环境变量. 在shell中执行程序时,shell会提供一组环境变量.export可新增,修改或删除环境变量,供后续执行的程序使用.export的效力仅及于该次 ...

  6. no_merge hint

    This is tested in 10gR2. SQL> select * from v$version; BANNER ----------------------------------- ...

  7. 关于Openstack的浅层次认知

    Openstack浅析 英文好的应该直接跳到官方文档去看相关的介绍,以下是具体介绍的连接,包含Openstack的具体架构: http://docs.openstack.org/kilo/instal ...

  8. CF #330 C

    改了题目之后,就是没有奇数的测试了... 其实可以很轻易地发现,要距离近的一方只会删除两端的,而要求远的一方会删除中间的. 那么,很明显的,剩下的两点会相差x/2个节点,于是,只要计算i和i+x/2的 ...

  9. HDU 5434

    其实是一道状态DP题.都是行与行之间的转移,可以知道,当某j列中有一个象,如果存在情况i-1行j-1列有象而i,j-1位置无象则不可放,或者i-1,j+1有而i,j+1无同样不可放. 使用快速状态转移 ...

  10. [WebGL入门]二十三,反射光的光照效果

    注:文章译自http://wgld.org/,原作者杉本雅広(doxas).文章中假设有我的额外说明.我会加上[lufy:],另外,鄙人webgl研究还不够深入.一些专业词语.假设翻译有误.欢迎大家指 ...