从零開始学android<TabHost标签组件.二十九.>
1
|
public TabHost(Context context)
|
构造
|
创建TabHost类对象
|
2
|
public void addTab(TabHost.TabSpec tabSpec)
|
普通
|
添加一个Tab
|
3
|
public TabHost.TabSpec newTabSpec(String tag)
|
普通
|
创建一个TabHost.TabSpec对象
|
4
|
public View getCurrentView()
|
普通
|
取得当前的View对象
|
5
|
public void setup()
|
普通
|
建立TabHost对象
|
6
|
public void setCurrentTab(int index)
|
普通
|
设置当前显示的Tab编号
|
7
|
public void setCurrentTabByTag(String tag)
|
普通
|
设置当前显示的Tab名称
|
8
|
public FrameLayout getTabContentView()
|
普通
|
返回标签容器
|
9
|
public void setOnTabChangedListener
(TabHost.OnTabChangeListener l)
|
普通
|
设置标签改变时触发
|
- <?
- 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" >
- <LinearLayout
- android:layout_marginTop="50dp"
- android:id="@+id/login"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TableRow
- android:id="@+id/tableRow1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="账号" />
- <EditText
- android:id="@+id/editText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:textSize="25sp" />
- </TableRow>
- <TableRow
- android:id="@+id/tableRow2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="password" />
- <EditText
- android:id="@+id/editText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:textSize="25sp" />
- </TableRow>
- <TableRow
- android:id="@+id/tableRow3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="取消" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="登录" />
- </TableRow>
- </LinearLayout>
- <LinearLayout
- android:layout_marginTop="50dp"
- android:id="@+id/image"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/a2" />
- </LinearLayout>
- <LinearLayout
- android:layout_marginTop="50dp"
- android:id="@+id/timer"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TimePicker
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:layout_marginTop="50dp"
- android:id="@+id/timer1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TimePicker
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:layout_marginTop="50dp"
- android:id="@+id/timer2"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TimePicker
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </LinearLayout>
- package com.example.tabhost;
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
- import android.widget.TabHost.TabSpec;
- public class MainActivity extends TabActivity {
- private TabHost tabHost;//初始化TabHost组件
- private int reslayout[] = { R.id.login, R.id.image, R.id.timer , R.id.timer1,R.id.timer2};//设置相应的额xml文件
- private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//设置显示的标题文件
- @SuppressWarnings("deprecation")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.tabHost = super.getTabHost();//实例化TabHost组件
- // 取得LayoutInflater对象
- LayoutInflater.from(this).inflate(R.layout.linearlayout,//制定布局
- this.tabHost.getTabContentView(),//制定标签添加的容器
- true);
- for (int i = 0; i < reslayout.length; i++) {
- TabSpec myTab = tabHost.newTabSpec("tab" + i);// 定义TabSpec
- myTab.setIndicator(null,getResources().getDrawable(images[i])) ; // 设置标签
- myTab.setContent(this.reslayout[i]) ; // 设置显示的组件
- this.tabHost.addTab(myTab) ;
- }
- }
- }
效果图例如以下
使用配置文件设置。
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/tabhost"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <RelativeLayout
- android:orientation="vertical"
- 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/login"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TableRow
- android:id="@+id/tableRow1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="账号" />
- <EditText
- android:id="@+id/editText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:textSize="25sp" />
- </TableRow>
- <TableRow
- android:id="@+id/tableRow2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="password" />
- <EditText
- android:id="@+id/editText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:textSize="25sp" />
- </TableRow>
- <TableRow
- android:id="@+id/tableRow3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="取消" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="登录" />
- </TableRow>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/image"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/a2" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/timer"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TimePicker
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/timer1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TimePicker
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/timer2"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TimePicker
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- </FrameLayout>
- </RelativeLayout >
- </TabHost>
JAVA文件配置
- package com.example.tabhost;
- import android.os.Bundle;
- import android.app.Activity;
- import android.widget.TabHost;
- import android.widget.TabHost.TabSpec;
- public class MainActivity extends Activity { // 直接继承Activity
- private TabHost myTabHost; // 定义TabHost
- private int[] layRes = { R.id.login, R.id.image
- , R.id.timer, R.id.timer1, R.id.timer2 }; // 定义内嵌布局管理器ID
- private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//标题图片数据
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.activity_main) ; // 调用默认布局管理器
- this.myTabHost = (TabHost) super.findViewById(R.id.tabhost); // 取得TabHost对象
- this.myTabHost.setup() ; // 建立TabHost对象
- for (int x = 0; x < this.layRes.length; x++) { // 循环取出全部布局标记
- TabSpec myTab = myTabHost.newTabSpec("tab" + x); // 定义TabSpec
- myTab.setIndicator(null,getResources().getDrawable(images[x])) ; // 设置标签文字
- myTab.setContent(this.layRes[x]) ; // 设置显示的组件
- this.myTabHost.addTab(myTab) ; // 添加标签
- }
- this.myTabHost.setCurrentTab(0) ; // 设置開始索引
- }
- }
从零開始学android<TabHost标签组件.二十九.>的更多相关文章
- 从零開始学android<SeekBar滑动组件.二十二.>
拖动条能够由用户自己进行手工的调节,比如:当用户须要调整播放器音量或者是电影的播放进度时都会使用到拖动条,SeekBar类的定义结构例如以下所看到的: java.lang.Object ↳ an ...
- 从零開始学android<Menu菜单组件.三十.>
在Android系统之中.菜单一共同拥有三类:选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 今天我们就用几个样例来分别介绍下菜单的使用 acti ...
- 从零開始学android<Bitmap图形组件.四十七.>
android.graphics.Bitmap(位图)是Android手机中专门提供的用于操作图片资源的操作类,使用此类能够直接从资源文件之中进行图片资源的读取.而且对这些图片进行一些简单的改动. 经 ...
- 从零開始学android<数据存储(1)SharedPreferences属性文件.三十五.>
在android中有五种保存数据的方法.各自是: Shared Preferences Store private primitive data in key-value pairs. 相应属性的键值 ...
- 第13章、布局Layouts之RelativeLayout相对布局(从零開始学Android)
RelativeLayout相对布局 RelativeLayout是一种相对布局,控件的位置是依照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最经常使用,也是最灵活的一种布 ...
- 从零開始学android<mediaplayer自带播放器(视频播放).四十九.>
MediaPlayer除了能够对音频播放之外,也能够对视频进行播放,可是假设要播放视频仅仅依靠MediaPlayer还是不够的.还须要编写一个能够用于视频显示的空间,而这块显示空间要求能够高速的进行G ...
- 从零開始学android<ImageSwitcher图片切换组件.二十六.>
ImageSwitcher组件的主要功能是完毕图片的切换显示,比如用户在进行图片浏览的时候.能够通过button点击一张张的切换显示的图片,并且使用ImageSwitcher组件在每次切换的时候也能够 ...
- 从零開始学android<RelativeLayout相对布局.十六.>
相对布局管理器指的是參考某一其它控件进行摆放,能够通过控制,将组件摆放在一个指定參考组件的上.下.左.右等位置,这些能够直接通过各个组件提供的属性完毕. 以下介绍一下各个方法的基本使用 No. 属性名 ...
- 从零開始学android<使用嵌套布局实现计算器界面.十七.>
所谓的嵌套布局就是在一个文件里嵌套多个布局文件 <span style="font-size:18px;"> <LinearLayout android:layo ...
随机推荐
- Android 推断程序在手机中是否是活动状态或者正在执行状态
沈阳斌子在今天项目需求上碰到个这种问题,在Service中须要推断当前的程序是否是活动状态,换句话说也就是说后台跑的服务中有业务需求检測当前程序是否是该服务的程序 这样好让点击推送通知时跳转到不同的页 ...
- 重构版机房收费系统之分层、接口、数据库连接、反射+工厂(vb.net)
分层 分层是为了减少层与层之间的依赖,添加程序的可读性,让整个系统结构清晰明白.还可大大减少维护成本,可是分层也有一定的缺点,有些能够直接訪问数据库的层,却要通过负责訪问数据库的层进行訪问.这样,在訪 ...
- 51nod-1363: 最小公倍数之和
[传送门:51nod-1363] 简要题意: 给出一个数n,求出1到n的数与n的最小公倍数的和 多组数据 题解: 理所当然推柿子 原题相当于求$\sum_{i=1}^{n}\frac{i*n}{gcd ...
- 求区间连续不超过K段的最大和--线段树+大量代码
题目描述: 这是一道数据结构题. 我们拥有一个长度为n的数组a[i]. 我们有m次操作.操作有两种类型: 0 i val:表示我们要把a[i]修改为val; 1 l r k:表示我们要求出区间[l,r ...
- php面向对象之__isset和__unset
php面向对象之__isset和__unset 一.简介 __isset和__unset都是对不可访问属性的操作,前者是检验的时候自动调用,后者是销毁的时候自动调用. 比如说在类外访问private的 ...
- HD-ACM算法专攻系列(14)——find your present (2)
问题描述: 源码: #include"iostream" #include"algorithm" using namespace std; bool cmp(i ...
- Core Java(五)
类和对象&方法 ——类的定义 现实世界的事物 属性:人的身高,体重等 行为:人可以学习,吃饭等 Java中用class描述事物也是如此 成员变量:就是事物的属性 成员方法:就是事物的行为 ...
- 卡片式大学综合英语词汇(Windows Phone 8.1 RT app)
简易卡片式记单词app.词库是原滋原味的大学综合英语词汇,包含语音,使用卡片式设计.离线词库,随时随地记单词. 商店:http://www.windowsphone.com/zh-cn/store/a ...
- Arduino扫盲(持续添加中)
1.Arduino火的很,很大一点在于,他基本透明掉了硬件电子部分,只剩下软件部分,通过把电子部分包装成黑箱,使得大量IT人士,普通人,甚至小学生也能玩的来. 2 .Arduino是一个电子原型开发平 ...
- 第五章 Python之装饰器
函数对象 函数是第一类对象:即函数可以当作数据传递 #可以被引用,可以被当作参数传递,返回值可以是函数,可以当作容器类型的元素 #引用 def func(x,y): print(x,y) f=func ...