本文实例讲述了android控件之tabhost用法。分享给大家供大家参考。具体如下:
以下通过tabhost实现android选项卡。
main.xml布局文件:
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
47
48
49
50
51
52
53
54
55
56
|
<?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" > <linearlayout android:id= "@+id/tab01" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:gravity= "center_horizontal" android:orientation= "vertical" > <imageview android:id= "@+id/iv01" android:scaletype= "fitxy" android:layout_gravity= "center" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/andy" /> <textview android:id= "@+id/tv01" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textsize= "24dip" android:text= "android的创造者: andy rubin" /> </linearlayout> <linearlayout android:id= "@+id/tab02" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:gravity= "center_horizontal" android:orientation= "vertical" > <imageview android:id= "@+id/iv02" android:scaletype= "fitxy" android:layout_gravity= "center" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/bill" /> <textview android:id= "@+id/tv02" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textsize= "24dip" android:text= "java创造者之一: bill joy" /> </linearlayout> <linearlayout android:id= "@+id/tab03" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:gravity= "center_horizontal" android:orientation= "vertical" > <imageview android:id= "@+id/iv03" android:scaletype= "fitxy" android:layout_gravity= "center" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/torvalds" /> <textview android:id= "@+id/tv03" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textsize= "24dip" android:text= "linux之父: linus torvalds" /> </linearlayout> </linearlayout> |
tabhostactivity类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.ljq.activity; import android.app.tabactivity; import android.os.bundle; import android.view.layoutinflater; import android.widget.tabhost; public class tabhostactivity extends tabactivity{ private tabhost tab= null ; @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); tab= this .gettabhost(); layoutinflater.from( this ).inflate(r.layout.main, tab.gettabcontentview(), true ); tab.addtab(tab.newtabspec( "选项卡一" ).setindicator( "选项卡一" , getresources().getdrawable(r.drawable.png1)).setcontent(r.id.tab01)); tab.addtab(tab.newtabspec( "选项卡二" ).setindicator( "选项卡二" , getresources().getdrawable(r.drawable.png2)).setcontent(r.id.tab02)); tab.addtab(tab.newtabspec( "选项卡三" ).setindicator( "选项卡三" , getresources().getdrawable(r.drawable.png3)).setcontent(r.id.tab03)); } } |
运行结果:
希望本文所述对大家的android程序设计有所帮助。