TabSpec与TabHost
Android游戏开发系统控件-TabSpec与TabHost
2012/5/12 星期六
5.12 汶川地震四周年,四年了,时间飞快,再大的苦难都属于过去,现在只着眼于眼前,把握现在,能让自己过得开心不后悔就行了。加油吧!!!
今天学习了另一个比较特殊的控件:TabSpec(分页),TabHost(分页的集合)
TabHost相当于浏览器中分页的集合,而TabSpec则相当于浏览器中的每个分页;在Android中,每一个TabSpec分页可以是一个组件,也可以是一个布局,然后将每个分页装入TabHost中,TabHost即可将其中的每个分页一并显示出来。
创建项目:TabProject
向项目资源中添加了两张图片资源:bg.png与bg2.png.
作者:wwj
功能:实现在布局中进行页面切换
项目运行结果截图:
修改代码:
=>>布局文件main.xml
- <?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"
- android:background="@drawable/bg2">
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/btn1"
- android:id="@+id/btn1"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/et1"
- android:id="@+id/et1"
- />
- <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="@string/btn2"
- />
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/et2"
- />
- </LinearLayout>
- </LinearLayout>
=>>string.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, TabProjectActivity!</string>
- <string name="app_name">TabProject</string>
- <string name="btn1">This is Tab1</string>
- <string name="btn2">This is Tab3</string>
- <string name="et1">This is Tab2</string>
- <string name="et2">This is Tab3</string>
- </resources>
=>>TabProjectActivity.java
- package com.tabHost;
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
- import android.widget.TabHost.OnTabChangeListener;
- import android.widget.TabHost.TabSpec;
- import android.widget.Toast;
- public class TabProjectActivity extends TabActivity implements OnTabChangeListener{
- private TabSpec ts1,ts2,ts3; //声明3个分页
- private TabHost tableHost; //分页菜单(tab容器)
- /** Called when the activity is first created. */
- @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.btn1);//设置此分页的资源id
- ts2 = tableHost.newTabSpec("tabTwo");
- //设置此分页显示的标题和图标
- ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.ic_launcher));
- ts2.setContent(R.id.et1);
- ts3 = tableHost.newTabSpec("tavThree");
- 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();
- }
- }
- }
自动添加的资源文件R.java
- /* AUTO-GENERATED FILE. DO NOT MODIFY.
- *
- * This class was automatically generated by the
- * aapt tool from the resource data it found. It
- * should not be modified by hand.
- */
- package com.tabHost;
- public final class R {
- public static final class attr {
- }
- public static final class drawable {
- public static final int bg=0x7f020000;
- public static final int bg2=0x7f020001;
- public static final int ic_launcher=0x7f020002;
- }
- public static final class id {
- public static final int btn1=0x7f050000;
- public static final int et1=0x7f050001;
- public static final int mylayout=0x7f050002;
- }
- public static final class layout {
- public static final int main=0x7f030000;
- }
- public static final class string {
- public static final int app_name=0x7f040001;
- public static final int btn1=0x7f040002;
- public static final int btn2=0x7f040003;
- public static final int et1=0x7f040004;
- public static final int et2=0x7f040005;
- public static final int hello=0x7f040000;
- }
- }
TabSpec与TabHost相关推荐
- TabSpec和TabHost实例
TabSpec与TabHost TabHost相当于浏览器中浏览器分布的集合,而Tabspec则相当于浏览器中的每一个分页面.d在Android中,每一个TabSpec分布可以是一个组件,也可以是一个 ...
- android基础之TabSpec和TabHost
代码如下: 布局代码: package com.example.tabhost; import android.app.TabActivity; import android.os.Bundle; i ...
- android中文api (59) —— TabHost.TabSpec
前言 本章内容是 android.widget.TabHost.TabSpec,版本为Android 2.3 r1,翻译来自"madgoat",欢迎大家访问他的博客:http:// ...
- android 中使用TabHost控件实现微信界面的底部菜单效果
首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置) <TabHostandroid:id="@android:id/tabhost"android:layout ...
- Android开发之自定义TabHost文字及背景(源代码分享)
使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢 MainActivity的源代码 package com.dream. ...
- Android之底部菜单TabHost的实现
/<span style="font-size:24px;">res/values/styles.xml代码:</span> <resources&g ...
- 【Android 应用开发】Android - TabHost 选项卡功能用法详解
TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105 . 作者 :万境绝尘 转载请注明出处 ...
- TabHost选项卡的 功能和用法
TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个外部容器相同大小的组件摆放区域 TabHost的主要组件是: TabWiget:代表一个选项卡标签条 TabSpec:代表选 ...
- TabHost选项卡的实现(一):使用TabActivity实现
一. TabHost的基本开发流程 TabHost是一种非常实用的组件,可以很方便的在窗口上防止多个标签页,每个标签页相当于获得了一个外部容器相同大小的组件摆放区域. 我们熟悉的手机电话系统" ...
最新文章
- 附加 集合数据_浩辰3D软件新手教程:三维建模设计中如何重用CAD模型数据?
- python3入门(三)字典的使用
- Linux查看所有用户和组信息
- spring框架所有包解释
- 澳大利亚.新西兰C#考试题
- 无法安装某些更新或程序
- ftp 501错误_分享,HTTP协议错误代码大全
- 爬虫之代理和cookie的处理
- 终极算法【6】——贝叶斯学派
- [含论文+答辩PPT+任务书+中期检查表+源码等]基于ssm的NBA球队管理系统
- Win10安装Centos7双系统
- 支付宝手机网站支付接口集成的经验小结
- 记录一下Base64 在线编码解码
- 情商 智商 逆商,哪个最重要?
- 鸿蒙生死印是谁的,逆天邪神:鸿蒙印的器灵还存在,或许云澈将知道些关于远古的秘密...
- 项目开发中遇到接收串口数据时序混乱的问题
- 百练2739:计算对数题解
- csapp2e 家庭作业 4.52 4.53
- 轮播图(火车轮播图)案例
- 服务器不能全屏显示,远程服务器如何全屏显示
热门文章
- 【设计模式】代理模式 ( 静态代理 )
- 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api
- 设计模式-Observer模式
- Codeforces 1201
- redis as session_handler
- 图片裁切,上传,自动匹配颜色。
- git简介 http://msysgit.github.io/
- NSArray和NSMutableArray
- 外行人都能看懂的SpringCloud,错过了血亏!
- 华为USG Firewall Ipsec L2L