快速搭建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) 在软件工程领域越来越红火 ...
随机推荐
- 比n大的最小不重复数
void Calculate(int a) { int pa = a; ; ] = {}; //将pa按位存储到数组b ) { b[count++] = pa%; pa = pa/; } bool f ...
- poj2385 - Apple Catching【动态规划】
Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...
- uva 10048 Audiophobia UVA - 10048
题目简介 一个无向正权图,求任意两个节点之间的路径里最短的路径长度. 直接Floyd解决,就是注意要把Floyd的DP式子改一下成 G[i][j]=min(G[i][j],max(G[i][k],G[ ...
- 仿探探卡片滑动vue封装并发布到npm
项目初始化使用 webpack-simple 方式比较方便和容易配置,原来的方式各种坑慎入 vue init webpack-simple vue-card-slide cd vue-card-sli ...
- Vue ui 大法哪家强?
Element iView Vuex Mint UI Vant cube-ui,对比六大 vue ui 组件库,选中最适合的那个. Element(pc) 介绍 & 版本 饿了么前端团队开发的 ...
- Asp.NET误人子弟教程:在MVC里面结合JQ实现AJAX
public class Person { public string Name { get; set; } public string City { get; set; } public strin ...
- Ubuntu_linux系统与网络服务管理
1.远程telnet联机:telnetd: 2.远程加密ssh联机:openssh: 3.webmin远程联机:webmin: 3.VNC远程图像界面控制:vino: 4.NAT网关服务器:iptab ...
- hdu 3500 DFS(限定)
Fling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...
- unigui的菜单树补习【2】treeview
Treeview用于显示按照树形结构进行组织的数据. Treeview控件中一个树形图由节点(TreeNode)和连接线组成.TtreeNode是TTreeview的基本组成单元. ...
- [luoguP3275] [SCOI2011]糖果(差分约束)
传送门 差分约束裸题 但是坑! 有一个点是长为10W的链,需要逆序加边才能过(真是玄学) 还有各种坑爹数据 开longlong ——代码 #include <cstdio> #includ ...