这篇文章主要介绍了Android开发之TabActivity用法,结合实例形式较为详细的分析了Android扩展Activity实现标签页效果的具体步骤与相关技巧,需要的朋友可以参考下
正文
Android开发之TabActivity用法实例详解
本文实例讲述了android开发之tabactivity用法。分享给大家供大家参考,具体如下:
一.简介
tabactivity继承自activity,目的是让同一界面容纳更多的内容。tabactivity实现标签页的功能,通过导航栏对各个页面进行管理。

二.xml布局文件
注意:
1.tabactivity的布局文件要求以tabhost作为xml布局文件的根。
2.通常我们采用线性布局,所以<tabhost> 的子元素是 <linearlayout>。
3.<tabwidget>对应tab
<framelayout>则用于包含tab需要展示的内容
需要注意的是<tabwidget> 和<framelayout>的id 必须使用系统id,分别为android:id/tabs 和 android:id/tabcontent 。
因为系统会使用者两个id来初始化tabhost的两个实例变量(mtabwidget 和 mtabcontent)。
4.代码示例
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version="1.0" encoding="utf-8"?><tabhost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <tabwidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"> </tabwidget> <framelayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"></framelayout></linearlayout></tabhost> |
三.tabactivity
1.tabhost:tabhost是tab的载体,用来管理tab。
2.tabhost的一些函数
(1)获取
|
1
|
tabhost tabhost=this.gettabhost(); |
(2) 创建tabhost.tabspec
|
1
|
public tabhost.tabspec newtabspec (string tag) |
(3)添加tab
|
1
|
public void addtab (tabhost.tabspec tabspec) |
(4)remove所有的tabs
|
1
2
|
public void clearalltabs ()public int getcurrenttab () |
(5) 设置当前的tab (by index)
|
1
|
public void setcurrenttab (int index) |
(6) 设置当前的(tab by tag)
|
1
|
public void setcurrenttabbytag (string tag) |
(7)设置tabchanged事件的响应处理
|
1
|
public void setontabchangedlistener (tabhost.ontabchangelistener l) |
3.tabhost.tabspec要设置tab的label和content,需要设置tabhost.tabspec类。tabhost.tabspec管理:
|
1
2
3
|
public string gettag ()public tabhost.tabspec setcontentpublic tabhost.tabspec setindicator |
(1)indicator这里的indicator 就是tab上的label,它可以
设置label :
|
1
|
setindicator (charsequence label) |
设置label和icon :
|
1
|
setindicator (charsequence label, drawable icon) |
指定某个view :
|
1
|
setindicator (view view) |
(2)content对于content ,就是tab里面的内容,可以
设置view的id :
|
1
|
setcontent(int viewid) |
用new intent 来引入其他activity的内容:setcontent(intent intent)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.zhanglong.music;import android.app.tabactivity;import android.content.intent;import android.content.res.resources;import android.os.bundle;import android.view.window;import android.view.windowmanager;import android.widget.tabhost;public class mainactivity extends tabactivity{ /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.main); resources res = getresources(); tabhost tabhost = gettabhost(); tabhost.tabspec spec; intent intent; intent = new intent().setclass(this, listactivity.class); spec = tabhost.newtabspec("音乐").setindicator("音乐", res.getdrawable(r.drawable.item)) .setcontent(intent); tabhost.addtab(spec); intent = new intent().setclass(this, artistsactivity.class); spec = tabhost.newtabspec("艺术家").setindicator("艺术家", res.getdrawable(r.drawable.artist)) .setcontent(intent); tabhost.addtab(spec); intent = new intent().setclass(this, albumsactivity.class); spec = tabhost.newtabspec("专辑").setindicator("专辑", res.getdrawable(r.drawable.album)) .setcontent(intent); tabhost.addtab(spec); intent = new intent().setclass(this, songsactivity.class); spec = tabhost.newtabspec("最近播放").setindicator("最近播放", res.getdrawable(r.drawable.album)) .setcontent(intent); tabhost.addtab(spec); tabhost.setcurrenttab(0); }} |
希望本文所述对大家android程序设计有所帮助。

发表评论