Android 常用UI控件之TabHost(2)简单示例
1,布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frgmt_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" > <TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="true" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@android:id/tabs"> <!-- 注意此句,如果没有此句,当tab1,tab2...过高时,会TabWidget会盖住 -->
<!-- tab1,用include可以引用别处的layout -->
<include
android:id="@+id/tab_weixin"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/tab_weixin_layout" />
<!-- tab2,用include可以引用别处的layout -->
<include
android:id="@+id/tab_contacts"
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="@layout/tab_contacts_layout" />
<!-- tab3,用include可以引用别处的layout -->
<include
android:id="@+id/tab_discovery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/tab_discovery_layout" />
<!-- tab4,用include可以引用别处的layout -->
<include
android:id="@+id/tab_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/tab_me_layout" />
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E0E0E0"
android:layout_alignParentBottom="true"
android:showDividers="none" >
</TabWidget>
</RelativeLayout>
</TabHost>
</FrameLayout>
2,在代码中初始化tab栏
void initTabHostTabs(LayoutInflater inflater){ tabHost.setup(); ScaleDrawable scale ;
TabSpec tab = tabHost.newTabSpec("weixin");
View tabView1 = inflater.inflate(R.layout.tab_indicator, null, false);
TextView tabTitle = (TextView) tabView1.findViewById(R.id.tab_title);
tabTitle.setText(R.string.tab_weixin);
ImageView tabIcon = (ImageView) tabView1.findViewById(R.id.tab_icon);
tabIcon.setImageResource(R.drawable.tab_weixin_scale);
scale = (ScaleDrawable) tabIcon.getDrawable();
scale.setLevel(); tab.setIndicator(tabView1);
tab.setContent(R.id.tab_weixin);
tabHost.addTab(tab); tab = tabHost.newTabSpec("contacts");
View tabView2 = inflater.inflate(R.layout.tab_indicator, null, false);
tabTitle = (TextView) tabView2.findViewById(R.id.tab_title);
tabTitle.setText(R.string.tab_contacts);
tabIcon = (ImageView) tabView2.findViewById(R.id.tab_icon);
tabIcon.setImageResource(R.drawable.tab_contacts_scale);
scale = (ScaleDrawable) tabIcon.getDrawable();
scale.setLevel(); tab.setIndicator(tabView2);
tab.setContent(R.id.tab_contacts);
tabHost.addTab(tab); tab = tabHost.newTabSpec("discovery");
View tabView3 = inflater.inflate(R.layout.tab_indicator, null, false);
tabTitle = (TextView) tabView3.findViewById(R.id.tab_title);
tabTitle.setText(R.string.tab_discovery);
tabIcon = (ImageView) tabView3.findViewById(R.id.tab_icon);
tabIcon.setImageResource(R.drawable.tab_discovery_scale);
scale = (ScaleDrawable) tabIcon.getDrawable();
scale.setLevel(); tab.setIndicator(tabView3);
tab.setContent(R.id.tab_discovery);
tabHost.addTab(tab); tab = tabHost.newTabSpec("me");
View tabView4 = inflater.inflate(R.layout.tab_indicator, null, false);
tabTitle = (TextView) tabView4.findViewById(R.id.tab_title);
tabTitle.setText(R.string.tab_me);
tabIcon = (ImageView) tabView4.findViewById(R.id.tab_icon);
tabIcon.setImageResource(R.drawable.tab_me_scale);
scale = (ScaleDrawable) tabIcon.getDrawable();
scale.setLevel(); tab.setIndicator(tabView4);
tab.setContent(R.id.tab_me);
tabHost.addTab(tab); /* 测试可以向右划动tab
for (int i = 0; i < 5; i++) {
tab = tabHost.newTabSpec("more" + i);
tab.setIndicator("more" + i);
tab.setContent(R.id.tab_me);
tabHost.addTab(tab);
}
*/
}
Android 常用UI控件之TabHost(2)简单示例的更多相关文章
- Android 常用UI控件之TabHost(5)Tab栏在底部且在最上层也不盖tab页
tab栏在底部 <TabHost android:id="@android:id/tabhost" android:layout_width="match_pare ...
- Android 常用UI控件之TabHost(1)TabHost的两种布局方式
TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...
- Android 常用UI控件之TabHost(4)实现当Tab栏有多个tab时,可以左右滑动
<!-- <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_wi ...
- Android 常用UI控件之TabHost(3)在4.0不显示图标的解决方案
1,自定义 TabWidget 上每个tab的view 2,用多个图片
- Android 常用UI控件之Tab控件的实现方案
实现Tab的方式有多种 1,ActionBar有两种模式可以实现,但是已经过期 tab模式tab在顶部,分裂模式tab在底部(同时所有action item都在底部). 2,PagerTitleStr ...
- 【风马一族_Android】第4章Android常用基本控件
第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...
- [置顶] Android常用适配器控件
Android常用适配器控件 列表控件用于显示数据集合,Android不是使用一种类型的控件管理显示和数据,而是将这两项功能分布用列表控件和适配器来实现.列表控件扩展了android.widget.A ...
- [Android] Android 让UI控件固定于底部的几种方法
Android 让UI控件固定于底部的几种方法1.采用linearlayout布局:android:layout_height="0dp" <!-- 这里不能设置fill_p ...
- widget 常用UI控件介绍
一.单选框 单选框实例程序: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
随机推荐
- mac os 10.10 pod install errors
/System/Library/Frameworks/Ruby.framework/Versions//gems/rake-/bin/rake RUBYARCHDIR=/Library/Ruby/Ge ...
- Xcode中添加代码块的方式
在写代码的过程中,经常会有重复的代码(比如说,cell的使用),当然了复制粘贴也不是不行,但是Xcode提供了一个很方便的东西. 1.在Xcode右下角你会看到有一个{}的东西,这里是一些常用的代码块 ...
- WTL 中的常见问题汇总
1.CRect,CPoint,CSize的使用 WTL提供了CString,CRect,CPoint和CSize,可能后来版本的ATL也提供了,WTL作者推荐使用ATL的实现,所以:#include ...
- 程序员面试题精选100题(38)-输出1到最大的N位数[算法]
作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. 分析:这 ...
- win2008 r2 远程桌面问题
今天去机房给三台服务器上架,装了2008 R2系统,客户要求从外面通过公网IP能够访问服务器桌面,三台服务器都安装了远程协助的功能,结果有两台能正常访问,另外一台始终连不上,不知道哪个地方设置有问题, ...
- 在Mac OS X中使用VIM开发STM32(1)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在我先前的博文⎣在Mac OS X中搭建STM32开发环境⎤中,我们在Mac中DIY出了最 ...
- 在Mac OS X中搭建STM32开发环境(1)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 本文方法必须好用!绝不坑爹!看了N多英文资料才搞明白的,适用于STM32F4DISCOVERY评估板,带 ...
- c++二分答案 之 跳石头
题目: 题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之 ...
- C# - 非中断(正常)模式下的调试
一般我们用Console.WriteLine()函数,将文本输出到控制台上来跟踪代码进行到了什么位置,局限性很大,适用范围窄. 1. 输出调试信息 命名空间 System.Dignostics Deb ...
- CAShapeLayer--备用
之前讲过CALayer动画相关知识,再来看看更加复杂的CAShapeLayer相关的动画知识. 普通CALayer在被初始化时是需要给一个frame值的,这个frame值一般都与给定view的boun ...