TabSpec和TabHost实例
来源:互联网 发布:淘宝刷好评兼职怎么做 编辑:程序博客网 时间:2024/05/20 03:44
TabSpec与TabHost
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
- page182
- linux系统监控(一)--- ps 详解
- 机锋2012第一季度移动APP支付数据解析
- 如何区分大小写、全角半角
- mac下配置apache支持多个端口
- TabSpec和TabHost实例
- Expression Language
- XP使用VNC远程桌面CentOS 6
- 用SAP Authority Object 对权限控制
- mscomm奇偶属性的设置
- IOS5 ARC unsafe_unretained等说明
- 配置MySQL主从复制(Replication)服务
- 后pc时代与想法
- Video for Linux