快速搭建tab
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的更多相关文章
- Jenkins+Maven+SVN快速搭建持续集成环境(转)
Jenkins是一个可扩展的持续集成引擎,Jenkins非常易于安装和配置,简单易用,下面看看我们是如何几分钟就快速搭建一个持续集成环境吧. 假设我们目前已经有2个maven项目:entities(J ...
- Ligerui首页的快速搭建
一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证码的实现(经常用,记录备用) 3.Ligerui首页的快速搭建 4.Ligerui Grid组件--学生信 ...
- Linux快速搭建C/C++开发环境
导读:越来越多的程序员在Linux下进行C/C++的开发.本文以CentOS 7为例,教你快速搭建一个vi + gcc/g++ + Make + valgrind的开发环境. 本文字数:1500,阅读 ...
- Nginx学习笔记--001-Nginx快速搭建
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- Github pages + jekyll 博客快速搭建
Github pages + jekyll 博客快速搭建 寻找喜欢的模版 https://github.com/jekyll/jekyll/wiki/sites http://jekyllthemes ...
- NodeJS 最快速搭建一个HttpServer
最快速搭建一个HttpServer 在目录里放一个index.html cd D:\Web\InternalWeb start http-server -i -p 8081
- 利用yeoman快速搭建React+webpack+es6脚手架
自从前后端开始分离之后,前端项目工程化也显得越来越重要了,之前写过一篇搭建基于Angular+Requirejs+Grunt的前端项目教程,有兴趣的可以点这里去看 但是有些项目可以使用这种方式,但有些 ...
- 基于Docker快速搭建多节点Hadoop集群--已验证
Docker最核心的特性之一,就是能够将任何应用包括Hadoop打包到Docker镜像中.这篇教程介绍了利用Docker在单机上快速搭建多节点 Hadoop集群的详细步骤.作者在发现目前的Hadoop ...
- 基于 Jenkins 快速搭建持续集成环境
什么是持续集成 随着软件开发复杂度的不断提高,团队开发成员间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.尤其是近些年来,敏捷(Agile) 在软件工程领域越来越红火 ...
随机推荐
- python爬虫20 | 小帅b教你如何使用python识别图片验证码
当你在爬取某些网站的时候 对于你的一些频繁请求 对方会阻碍你 常见的方式就是使用验证码 验证码的主要功能 就是区分你是人还是鬼(机器人) 人 想法设法的搞一些手段来对付技术 而 技术又能对付人们的想法 ...
- 如何在docker和宿主机之间复制文件
如何在docker和宿主机之间复制文件 最近在用Docker布署hadoop,要将文件上传到HDFS首先文件得在Docker容器中吧,网上提供的方法差不多有三种 1.用-v挂载主机数据卷到容器内 ...
- 使用Autofac 依赖注入及 swagger 之startup配置
言语有限,代码如下: public IServiceProvider ConfigureServices(IServiceCollection services) { services .AddCor ...
- java中redis的分布式锁工具类
使用方式 try { if(PublicLock.getLock(lockKey)){ //这里写代码逻辑,执行完后需要释放锁 PublicLock.freeLock(lockKey); } } ca ...
- wmpnetwk.exe怎么禁启动
Win7控制面板--管理工具--服务,找到并选中"Windows Media Player Network Sharing Service"服务,右键点"属性" ...
- Q - Period II
For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...
- Hibernate数据缓存攻略
目录 第一章 缓存策略概述 第二章 hibernate不使用缓存的问题 第三章 一级缓存介绍 第四章 二级缓存 第五章 一二级缓存对比及总结 源代码: https://github.com/weili ...
- VBox虚拟机与主机(宿主)通讯原理以及socat(套接字猫)简单介绍
前言 尝试虚拟机使用socat建立服务器端接口转发时,发现对虚拟机接入网络原理不是非常了解,于是乎上网查找资料想搞明白是怎么回事,于是乎有了这篇总结博文.socat可以在服务器端口间建立全双工通信通道 ...
- Tween公式
Tween公式 4个参数 t:current time(当前时间) b:beginning value(初始值) c: change in value(变化量) d:duration(持续时间) re ...
- iOS中UITextView的操作技巧
刚才看了一篇textView实现placeholder的文章,有兴趣的同学们能够看下:__biz=MzA3NzM0NzkxMQ==&mid=211846438&idx=1&sn ...