Android游戏开发系统控件-TabSpec与TabHost

2012/5/12 星期六

5.12 汶川地震四周年,四年了,时间飞快,再大的苦难都属于过去,现在只着眼于眼前,把握现在,能让自己过得开心不后悔就行了。加油吧!!!

今天学习了另一个比较特殊的控件:TabSpec(分页),TabHost(分页的集合)

TabHost相当于浏览器中分页的集合,而TabSpec则相当于浏览器中的每个分页;在Android中,每一个TabSpec分页可以是一个组件,也可以是一个布局,然后将每个分页装入TabHost中,TabHost即可将其中的每个分页一并显示出来。

创建项目:TabProject

向项目资源中添加了两张图片资源:bg.png与bg2.png.

作者:wwj

功能:实现在布局中进行页面切换

项目运行结果截图:

修改代码:

=>>布局文件main.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:background="@drawable/bg2">
  7. <Button
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/btn1"
  11. android:id="@+id/btn1"
  12. />
  13. <EditText
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="@string/et1"
  17. android:id="@+id/et1"
  18. />
  19. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  20. android:orientation="vertical"
  21. android:layout_width="fill_parent"
  22. android:layout_height="fill_parent"
  23. android:id="@+id/mylayout"
  24. android:background="@drawable/bg"
  25. >
  26. <Button
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:text="@string/btn2"
  30. />
  31. <EditText
  32. android:layout_width="fill_parent"
  33. android:layout_height="wrap_content"
  34. android:text="@string/et2"
  35. />
  36. </LinearLayout>
  37. </LinearLayout>

=>>string.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, TabProjectActivity!</string>
  4. <string name="app_name">TabProject</string>
  5. <string name="btn1">This is Tab1</string>
  6. <string name="btn2">This is Tab3</string>
  7. <string name="et1">This is Tab2</string>
  8. <string name="et2">This is Tab3</string>
  9. </resources>

=>>TabProjectActivity.java

[java] view plaincopy
  1. package com.tabHost;
  2. import android.app.TabActivity;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.widget.TabHost;
  6. import android.widget.TabHost.OnTabChangeListener;
  7. import android.widget.TabHost.TabSpec;
  8. import android.widget.Toast;
  9. public class TabProjectActivity extends TabActivity implements OnTabChangeListener{
  10. private TabSpec ts1,ts2,ts3;    //声明3个分页
  11. private TabHost tableHost;      //分页菜单(tab容器)
  12. /** Called when the activity is first created. */
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. tableHost = this.getTabHost();//实例(分页)菜单
  17. //利用LayoutInflater将布局与分页菜单一起显示
  18. LayoutInflater.from(this).inflate(R.layout.main, tableHost.getTabContentView());
  19. ts1 = tableHost.newTabSpec("tabOne");//实例化一个分页
  20. ts1.setIndicator("Tab1");//设置此页显示的标题
  21. ts1.setContent(R.id.btn1);//设置此分页的资源id
  22. ts2 = tableHost.newTabSpec("tabTwo");
  23. //设置此分页显示的标题和图标
  24. ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.ic_launcher));
  25. ts2.setContent(R.id.et1);
  26. ts3 = tableHost.newTabSpec("tavThree");
  27. ts3.setIndicator("Tab3");
  28. ts3.setContent(R.id.mylayout);//设置此分页的布局id
  29. tableHost.addTab(ts1);//菜单中添加ts1分页
  30. tableHost.addTab(ts2);
  31. tableHost.addTab(ts3);
  32. tableHost.setOnTabChangedListener(this);//这次监听器
  33. }
  34. public void onTabChanged(String tabId){
  35. if(tabId.equals("tabOne")){
  36. Toast.makeText(this, "分页1", Toast.LENGTH_LONG).show();
  37. }
  38. if(tabId.equals("tabTwo")){
  39. Toast.makeText(this, "分页2", Toast.LENGTH_LONG).show();
  40. }
  41. if(tabId.equals("tabThree")){
  42. Toast.makeText(this,"分页3",Toast.LENGTH_LONG).show();
  43. }
  44. }
  45. }

自动添加的资源文件R.java

[java] view plaincopy
  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY.
  2. *
  3. * This class was automatically generated by the
  4. * aapt tool from the resource data it found.  It
  5. * should not be modified by hand.
  6. */
  7. package com.tabHost;
  8. public final class R {
  9. public static final class attr {
  10. }
  11. public static final class drawable {
  12. public static final int bg=0x7f020000;
  13. public static final int bg2=0x7f020001;
  14. public static final int ic_launcher=0x7f020002;
  15. }
  16. public static final class id {
  17. public static final int btn1=0x7f050000;
  18. public static final int et1=0x7f050001;
  19. public static final int mylayout=0x7f050002;
  20. }
  21. public static final class layout {
  22. public static final int main=0x7f030000;
  23. }
  24. public static final class string {
  25. public static final int app_name=0x7f040001;
  26. public static final int btn1=0x7f040002;
  27. public static final int btn2=0x7f040003;
  28. public static final int et1=0x7f040004;
  29. public static final int et2=0x7f040005;
  30. public static final int hello=0x7f040000;
  31. }
  32. }

TabSpec与TabHost相关推荐

  1. TabSpec和TabHost实例

    TabSpec与TabHost TabHost相当于浏览器中浏览器分布的集合,而Tabspec则相当于浏览器中的每一个分页面.d在Android中,每一个TabSpec分布可以是一个组件,也可以是一个 ...

  2. android基础之TabSpec和TabHost

    代码如下: 布局代码: package com.example.tabhost; import android.app.TabActivity; import android.os.Bundle; i ...

  3. android中文api (59) —— TabHost.TabSpec

    前言 本章内容是 android.widget.TabHost.TabSpec,版本为Android 2.3 r1,翻译来自"madgoat",欢迎大家访问他的博客:http:// ...

  4. android 中使用TabHost控件实现微信界面的底部菜单效果

    首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置) <TabHostandroid:id="@android:id/tabhost"android:layout ...

  5. Android开发之自定义TabHost文字及背景(源代码分享)

    使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢 MainActivity的源代码 package com.dream. ...

  6. Android之底部菜单TabHost的实现

    /<span style="font-size:24px;">res/values/styles.xml代码:</span> <resources&g ...

  7. 【Android 应用开发】Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  8. TabHost选项卡的 功能和用法

    TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个外部容器相同大小的组件摆放区域 TabHost的主要组件是: TabWiget:代表一个选项卡标签条 TabSpec:代表选 ...

  9. TabHost选项卡的实现(一):使用TabActivity实现

    一. TabHost的基本开发流程 TabHost是一种非常实用的组件,可以很方便的在窗口上防止多个标签页,每个标签页相当于获得了一个外部容器相同大小的组件摆放区域. 我们熟悉的手机电话系统" ...

最新文章

  1. 附加 集合数据_浩辰3D软件新手教程:三维建模设计中如何重用CAD模型数据?
  2. python3入门(三)字典的使用
  3. Linux查看所有用户和组信息
  4. spring框架所有包解释
  5. 澳大利亚.新西兰C#考试题
  6. 无法安装某些更新或程序
  7. ftp 501错误_分享,HTTP协议错误代码大全
  8. 爬虫之代理和cookie的处理
  9. 终极算法【6】——贝叶斯学派
  10. [含论文+答辩PPT+任务书+中期检查表+源码等]基于ssm的NBA球队管理系统
  11. Win10安装Centos7双系统
  12. 支付宝手机网站支付接口集成的经验小结
  13. 记录一下Base64 在线编码解码
  14. 情商 智商 逆商,哪个最重要?
  15. 鸿蒙生死印是谁的,逆天邪神:鸿蒙印的器灵还存在,或许云澈将知道些关于远古的秘密...
  16. 项目开发中遇到接收串口数据时序混乱的问题
  17. 百练2739:计算对数题解
  18. csapp2e 家庭作业 4.52 4.53
  19. 轮播图(火车轮播图)案例
  20. 服务器不能全屏显示,远程服务器如何全屏显示

热门文章

  1. 【设计模式】代理模式 ( 静态代理 )
  2. 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api
  3. 设计模式-Observer模式
  4. Codeforces 1201
  5. redis as session_handler
  6. 图片裁切,上传,自动匹配颜色。
  7. git简介 http://msysgit.github.io/
  8. NSArray和NSMutableArray
  9. 外行人都能看懂的SpringCloud,错过了血亏!
  10. 华为USG Firewall Ipsec L2L