Android TabHost的使用
标签显示界面的主要特点是可以在一个窗口中显示多组标签栏的类容。
在Android系统中,每个标签栏称为一个Tab,而包含多个标签栏的内容就称为TabHost。
通过TabHost的继承结构来看,TabHost类是FrameLayout的子类。
实现标签显示界面有两种方式可供选择。
- 直接让一个Activity继承TabActivity类。
- 利用findViewById()方法取得TabHost组件,并进行一些配置。
下面我们用两个简单例子来体验一下:
第一种继承TabActivity类:(注意:TabActivity类已被官方提出抛弃)
首先就是布局文件tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_edit"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入检索关键字..."
android:textSize="18px" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_clock"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<AnalogClock
android:id="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_sex"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/male"
android:checked="true"
android:text="性别:男" />
<RadioButton
android:id="@+id/female"
android:text="性别:女" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
其次就是默认的MainActivity的实现:
public class MyTabHostDemo extends TabActivity { // 继承了TabActivity
private TabHost myTabHost;
private int[] layRes = new int[] { R.id.tab_edit, R.id.tab_clock,
R.id.tab_sex }; // 这些是内嵌布局文件的ID
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.myTabHost = super.getTabHost(); // 取得TabHost对象
LayoutInflater.from(this).inflate(R.layout.tab,
this.myTabHost.getTabContentView(), true); // true表示实例化布局文件中的组件
for (int x = 0; x < this.layRes.length; x++) {
TabSpec myTab = this.myTabHost.newTabSpec("tab" + x) ;
myTab.setIndicator("标签 - " + x) ;
myTab.setContent(this.layRes[x]) ;
this.myTabHost.addTab(myTab) ;
}
}
}
到此,一个简单的TabHost的使用就完成了。
第二种方式的实现:
首先,我们还是是布局文件的实现。
tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TabHost01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" >
</TextView>
</LinearLayout> <LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="two" >
</TextView>
</LinearLayout> <LinearLayout
android:id="@+id/LinearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/TextView03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="three" >
</TextView>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</LinearLayout> </TabHost>
main.xml:(这个不是默认.JAVA文件加载的布局文件,只是为了方便没有新建布局文件。)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nihao!!!"/> </LinearLayout>
实现默认的MainActivity:
public class MainActivity extends ActivityGroup {//ActivityGroup类也已被建议遗弃
private TabHost tabHost;
Intent oneIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
oneIntent = new Intent();
oneIntent.setClass(this, testActivity.class);
tabHost = (TabHost) this.findViewById(R.id.TabHost01);//获得对象
tabHost.setup();//建立对象(与上一行代码必须同时存在)
tabHost.setup(this.getLocalActivityManager());//要实现Intent跳转,必须要这句话。
try {
tabHost.addTab(tabHost
.newTabSpec("tab_1")
.setContent(R.id.LinearLayout1)
.setIndicator("TAB1",
this.getResources().getDrawable(R.drawable.img1)));
tabHost.addTab(tabHost
.newTabSpec("tab_2")
.setContent(R.id.LinearLayout2)
.setIndicator("TAB2",
this.getResources().getDrawable(R.drawable.img2)));
tabHost.addTab(tabHost.newTabSpec("tab_3")
.setContent(R.id.LinearLayout3).setIndicator("TAB3"));
tabHost.addTab(tabHost
.newTabSpec("tab_4")
.setIndicator("TAB4",
this.getResources().getDrawable(R.drawable.img3))
.setContent(oneIntent));
tabHost.setCurrentTab(1); // 设置默认的页面
} catch (Exception ex) {
ex.printStackTrace();
Log.d("EXCEPTION", ex.getMessage());
}
}
}
新建一个用于Intent跳转的Activity:
public class testActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView) findViewById(R.id.TV);
tv.setText("已经跳转了");
}
}
接下来运行即可。
这样,用两个例子分别实现了两种不同的实现方式。
Android TabHost的使用的更多相关文章
- Android tabhost下的activity怎样获取传来的值
android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); int ...
- Android - TabHost 与 Fragment 制作页面切换效果
Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...
- Android——TabHost(标签容器)相关知识总结贴
android 2.3 r1 中文 api (58) —— TabHost http://www.apkbus.com/android-18911-1-1.html android中文api (5 ...
- Android TabHost使用
TabHost是Android中自带的选项卡控件,效果图如下: 主布局文件 <RelativeLayout xmlns:android="http://schemas.android. ...
- Android -- TabHost
TabHost 也就相当于Windows下的选项框 有两种实现方式 1. 继承TabActivity (已经废弃):从TabActivity中用getTabHost()方法获取TabHost 2. ...
- Android -- TabHost、Fragment、状态保存、通信
工程结构 TabAFm到Ta ...
- Android TabHost中Activity之间传递数据
例子1: TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost); tabhost.setup(this.getLocalActi ...
- Android TabHost 动态修改图标或者动态改变标题
那时客户需要实现在TabHost标题上动态显示从数据库获取的个数.起初这样思考的,从数据库 获取个数是非常简单,但是要把获取的个数显示在TabHost标题,思前想后,想用Handler来异步实现消息传 ...
- Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...
- Android TabHost TabWidget 去除黑线(底部下划线)
采用TabHost布局时,往往会发现默认的系统风格与软件风格很不协调,比如TabWidget的下划线影响布局效果.通常情况下会去除其下划线.如果是采用xml布局文件,在TabWidget的属性项设置a ...
随机推荐
- 在DDMS中查看网络使用详情
在Android 4.0设置中的“流量使用情况”允许长期统计每个App如何使用网络资源.从4.0.3开始,配合最新发布的DDMS r17(在ADT r17 插件中有集成),您可以实时的在DDMS中查看 ...
- 自动测试框架(by myself)
这段日子以来一直在自动话测试,然后关于框架一直有个很模糊的概念,通过N多人的解说,这个应该不能算是一个框架,但是还是很模糊 如下图是我自己认为的框架,不知道是否正确(请大侠们多多指点) 1.用nuni ...
- 【HTML】Beginner4:Heading
1.Headings h1 h2 h3 h4 h5 h6 h1 being the almighty emperor of headings h6 being the lowest p ...
- codeforce 621C Wet Shark and Flowers
题意:输入个n和质数p,n个区间,每个区间可以等概率的任选一个数,如果选的这个区间和它下个区间选的数的积是p的倍数的话(n的下个是1),就挣2000,问挣的期望 思路:整体的期望可以分成每对之间的期望 ...
- Java和.NET的GZIP压缩功能对比
本文主要比较了Java和.NET提供的GZIP压缩功能. 介绍 在本文中,我们将讨论Java和.NET提供的GZIP压缩功能,并且用实例来说明哪个压缩方法更佳. 在Java中,我们有提供GZIP压缩的 ...
- 问题-delphi XE2 Stack Overflow- save your work and restart CodeGear
问题现象:某一天,启动DLEPHI XE2 后,新建一个工程,双击一个事件,“Stack Overflow- save your work and restart CodeGear delphi xe ...
- iOS开发:创建真机调试证书
关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...
- SVN版本控制安装配置说明
版本控制好工具有SVN.CVS.VSS等多种,他们的优劣在此不说明,请网络参阅. SVN支持多种平台,此文仅描述Windows平台下使用说明. SVN客户包含客户端和服务端.Windows平台下客户端 ...
- cluster模块实现多进程-让我的代理服务速度飞起来了
cluster模块实现多进程 现在的cluster已经可以说完全做到的负载均衡,在做代理服务和http服务器的时候能够讲服务器性能发挥到最大.来看一下具体的实现吧 var cluster = requ ...
- MongoDB--使用修改器修改文档
可以使用修改器啦修改文档,比如增加.删除文档的键值.使用修改器首先要定位到某个文档, 然后再增加相应的修改选项,需要使用update语句 1.$inc修改器修改文档 > db.users.fin ...