TabSpec和TabHost实例
来源:互联网 发布:名片设计软件免费版 编辑:程序博客网 时间:2024/05/23 09:35
原来地址:http://blog.csdn.net/myserverthepeople/article/details/7630052
TabHost相当于浏览器中浏览器分布的集合,而Tabspec则相当于浏览器中的每一个分页面。d在Android中,每一个TabSpec分布可以是一个组件,也可以是一个布局,然后将每一个分页装入TabHost中,TabHost即可将其中的每一个分页一并显示出来。
步骤:
(1)继承TabActivity:在此之前继承的都是android.app.Activity类,但是这里需要继承android.app.TabActivity。
(2)创建TabHost分布菜单对象,利用以下代码。
LayoutInflater.from(this).inflate(R.layout.main,tableHost.getTabContentView());
(3)实例化实分页
java代码:
- package com.test;
- import android.app.Activity;
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
- import android.widget.TabHost.OnTabChangeListener;
- import android.widget.Toast;
- import android.widget.TabHost.TabSpec;
- public class MainActivity extends TabActivity implements OnTabChangeListener {
- /** Called when the activity is first created. */
- private TabSpec ts1 ,ts2, ts3 ;//声明3个分页
- private TabHost tableHost;//分页菜单(tab的容器)
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- tableHost = this.getTabHost();//实例 (分页)菜单
- //利用LayoutInflater将布局与分页菜单一起显示
- LayoutInflater.from(this).inflate(R.layout.main, tableHost.getTabContentView());
- ts1 = tableHost.newTabSpec("tabOne");//实例化一个分页
- ts1.setIndicator("Tab1");//设置此分页显示的标题
- ts1.setContent(R.id.btn);//设置此分页的资源id
- ts2=tableHost.newTabSpec("tabTwo");//实例化第二个分页
- //设置此分页显示的标题和图标
- ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.icon));
- ts2.setContent(R.id.et);
- ts3= tableHost.newTabSpec("tabThree");//实例化第三个分页
- ts3.setIndicator("Tab3");
- ts3.setContent(R.id.mylayout);//设置此分页的布局id
- tableHost.addTab(ts1);//菜单中添加ts1分页
- tableHost.addTab(ts2);
- tableHost.addTab(ts3);
- tableHost.setOnTabChangedListener(this);
- }
- public void onTabChanged(String tabId){
- if(tabId.equals("tabOne")){
- Toast.makeText(this, "分页1", Toast.LENGTH_LONG).show();
- }
- if(tabId.equals("tabTwo")){
- Toast.makeText(this, "分页2", Toast.LENGTH_LONG).show();
- }
- if(tabId.equals("tabThree")){
- Toast.makeText(this, "分页3", Toast.LENGTH_LONG).show();
- }
- }
- }
布局代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="This is Tab1"
- />
- <EditText
- android:id="@+id/et"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is Tab2"
- />
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/mylayout"
- android:background="@drawable/bg"
- >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is Tab3"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is Tab3"
- />
- </LinearLayout>
- </LinearLayout>
运行结果:
总结:监听分页改变事件,具体如下:
1、使用OnTabChangeListener接口,重写OnTabChanged(String tabId)函数
2、TabHost绑定监听器
3、判断OnTabChanged(String tabId)中的tabId参数进行处理事件;这里的tabId对应的是实例中每个分页传入的分页ID,而不是TabSpec.setIndicatior()设置的标题
- TabSpec和TabHost实例
- TabSpec和TabHost实例
- TabSpec和TabHost实例
- TabSpec与TabHost
- Android游戏开发系统控件-TabSpec与TabHost
- android中文api (59) —— TabHost.TabSpec
- tabhost TabSpec执行setContent时出现空指针异常NullPointerException
- tabhost实例
- tabhost 实例
- TabHost.TabSpec中的标签实现图文左右排(默认上下排)
- tabhost中setup()和setup(LocalActivityManager activityGroup)实例创建区别
- android 在popupWindow中添加tabHost控件,tabspec中listView控件无法响应触屏事件问题?
- TabHost使用实例
- Android实例 - TabHost
- TabHost和Tab
- TabHost和Tab
- ViewPager和TabHost结合
- TabHost和RadioGroup
- UVA - 10491 Cows and Cars
- 移植OpenCV到ARM时的注意事项
- gps gprs sim300 发短信
- 【安杰秀坊】-001- 动态设置DrawableLeft|Top|Right|Bottom图片
- C语言 南阳理工ACM 67.三角形面积
- TabSpec和TabHost实例
- 在相恋80年后,一对乌拉圭恋人终于结婚了
- 一个高手总结的单片机设计与KeilC编程总结 (转载)
- 推荐两本有关经济学的书籍
- osx10.9 xcode5升级后的老代码环境变通
- c++ 内存管理:5、指针参数是如何传递内存的
- 期待并实现我的大学生活
- IO总结(三)
- 归并分组