快速搭建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) 在软件工程领域越来越红火 ...
随机推荐
- Codevs P1017 乘积最大
P1017 乘积最大 题目描述 Description 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的 ...
- 1.1 Java程序设计平台
Java并不只是一种语言.在此之前出现的那么多中语言也没有能够引起那么大的轰动.Java是一个完整的平台,有一个庞大的库,其中包含了很多可重用的代码和一个提供诸如安全性.跨操作系统的可移植性以及自动垃 ...
- 学习Spring框架等技术的方向、方法和动机
学习Spring框架最早学习Spring框架是在大二的时候,当时看了几本书,看了一些视频,主要是传智播客的.更多的,还是写代码,单独写Spring的,也有与Struts和Hibernate等框架整合的 ...
- Django获取当前页面的URL——小记
获取带参数URL:request.get_all_path() 获取不带参数URL:request.path 获取主机地址:request.get_host()
- hdu_1021_Fibonacci Again_201310232237
Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- systemtap dtrace与 oracle
https://fritshoogland.wordpress.com/page/3/ http://externaltable.blogspot.com/2013/06/dtrace-explora ...
- 为什么视频流一般都用UDP
我们知道TCP 和 UDP的一个区别之一就是TCP要保证丢失的package会被再次重发,确保对方能够收到. 而在视频播放中,如果有一秒钟的信号确实,导致画面出现了一点瑕疵,那么最合适的办法是把这点瑕 ...
- python network programming--connect()
首先我们看一段python client/server代码. server端: >>> import sys,socket >>> s = socket.socke ...
- Qt移动应用开发(一):适配不同的屏幕
Qt移动应用开发(一):适配不同的屏幕 到眼下为止.Qt5.3已经出现非常长一段时间了.而且已经有一些应用使用Qt进行构建了.我自己也完毕了第一款使用Qt构建的手机游戏<吃药了>.那么接下 ...
- gradle配置国内的镜像
gradle配置国内的镜像 学习了:http://blog.csdn.net/stdupanda/article/details/72724181 http://blog.csdn.net/lj402 ...