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 ...
随机推荐
- 基于WebForm+EasyUI的业务管理系统形成之旅 -- 施工计划安排(Ⅶ)
上篇<基于WebForm+EasyUI的业务管理系统形成之旅 -- 首页Portal界面拖拽>,主要介绍首页随客户喜好安排区块位置,更好的实现用户体验. 这两天将项目中施工计划管理归纳总结 ...
- Add controls dynamically in flowlayoutpanel
For a FlowLayoutPanel, you don't need to specify a location since the controls are arranged for you. ...
- C# 日期转换为中文大写
/// <summary> /// 日期转换为中文大写 /// </summary> public class UpperConvert { public UpperConve ...
- 【原】Comparator和Comparable的联系与区别
1.知识点了解 Comparator和Comparable都是用用来实现集合中元素的比较.排序的,所以,经常在集合外定义Comparator接口的方法和集合内实现Comparable接口的方法中实现排 ...
- bzoj 1483 [HNOI2009]梦幻布丁(链表+启发式合并)
1483: [HNOI2009]梦幻布丁 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1818 Solved: 761[Submit][Status ...
- POJ 3279 Fliptile (质量不错)
题意:有一个M*N的棋盘,每一个格子只有两种状态0或1,每次可以选择一个格子执行翻转操作,并且与该格子相邻的4个格子都会被翻转,求将所有格子都翻转成0所需要的最小操作数,若有多种方案,输出字典序最小的 ...
- Android: Dragging Popup Window 可移动浮动View
final View cv = new View(this); setContentView(cv); TextView tv = new TextView(this); tv.setBackgrou ...
- cloud-utils cloud-utils-growpart cloud-init
- 学习并使用了两种linq to entity 的实现sql关键字in的查询方法
//构造Lambda语句 private static Expression<Func<TElement, bool>> BuildWhereInExpressi ...
- bootstrap多层模态窗
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...