第一种,TabActivity 解决方案
下面建立的布局文件,它包含多个标签的显示组件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/MyLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id ="@+id/tab_edit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id ="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize= "18px"
android:text = "请输入检索关键字"
/>
<Button
android:id = "@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "搜索"
/>
</LinearLayout>
<LinearLayout
android:id ="@+id/tab_clock"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical" >
<AnalogClock
android:id ="@+id/myAnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id ="@+id/tab_sex"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id ="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id ="@+id/male"
android:checked = "true"
android:text = "性别 : 男"
/>
<RadioButton
android:id ="@+id/female"
android:text = "性别 : 女"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
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
|
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabHostDemo extends TabActivity {
private TabHost myTabHost;
private int [] layRes = new int []{R.id.tab_edit,R.id.tab_clock,
R.id.tab_sex};
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
//setContentView(R.layout.tab);
this .myTabHost = super .getTabHost(); //取得TabHost对象
LayoutInflater.from( this ).inflate(R.layout.tab,
this .myTabHost.getTabContentView(), true );
for ( int x= 0 ; x < this .layRes.length; x++){
TabSpec myTab = myTabHost.newTabSpec( "tab" + x);
myTab.setIndicator( "标签 -" +x);
myTab.setContent( this .layRes[x]);
this .myTabHost.addTab(myTab);
}
}
}
|
第二种 表格标签(继承Activity)利用Tab.xml布局
1 <?xml version="1.0" encoding="utf-8"?>
2 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id ="@+id/tabhost"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical" >
7 <LinearLayout
8 android:orientation="vertical"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 >
12
13
14 <TabWidget
15 android:id ="@android:id/tabs"
16 android:layout_width="fill_parent"
17 android:layout_height="wrap_content"
18 android:layout_alignParentTop="true"
19 />
20 <FrameLayout
21 android:id="@android:id/tabcontent"
22 android:layout_width="fill_parent"
23 android:layout_height="fill_parent"
24 >
25 <LinearLayout
26 android:id ="@+id/tab_edit"
27 android:layout_width="fill_parent"
28 android:layout_height="fill_parent"
29 android:orientation="vertical" >
30
31
32 <EditText
33 android:id ="@+id/edit"
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:textSize= "18px"
37 android:text = "请输入检索关键字"
38 />
39 <Button
40 android:id = "@+id/but"
41 android:layout_width="wrap_content"
42 android:layout_height="wrap_content"
43 android:text = "搜索"
44 />
45 </LinearLayout>
46 <LinearLayout
47 android:id ="@+id/tab_clock"
48 android:layout_width="fill_parent"
49 android:layout_height="fill_parent"
50 androidrientation="vertical" >
51
52
53 <AnalogClock
54 android:id ="@+id/myAnalogClock"
55 android:layout_width="wrap_content"
56 android:layout_height="wrap_content"
57
58 />
59
60 </LinearLayout>
61
62
63 <LinearLayout
64 android:id ="@+id/tab_sex"
65 android:layout_width="fill_parent"
66 android:layout_height="fill_parent"
67 android:orientation="vertical" >
68
69
70 <RadioGroup
71 android:id ="@+id/sex"
72 android:layout_width="fill_parent"
73 android:layout_height="wrap_content"
74 android:orientation="vertical"
75 >
76 <RadioButton
77 android:id ="@+id/male"
78 android:checked = "true"
79 android:text = "性别 : 男"
80 />
81 <RadioButton
82 android:id ="@+id/female"
83 android:text = "性别 : 女"
84 />
85 </RadioGroup>
86 </LinearLayout>
87 </FrameLayout>
88 </LinearLayout>
89 </TabHost>
1 package cn.TabHost;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.widget.TabHost;
6 import android.widget.TabHost.TabSpec;
7
8 public class TabHostDemo extends Activity {
9 private TabHost myTabHost;
10 private int[] layRes = new int[]{R.id.tab_edit,R.id.tab_clock,
11 R.id.tab_sex};
12
13 @Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.tab);
17
18 myTabHost = (TabHost)findViewById(R.id.tabhost);
19 myTabHost.setup(); //建立TabHost对象
20 for(int x=0; x < this.layRes.length; x++ ) {
21 TabSpec myTab = myTabHost.newTabSpec("tag" + x);
22 myTab.setIndicator("标签 - " + x);
23 myTab.setContent(this.layRes[x]);
24 myTabHost.addTab(myTab);
25
26 }
27 this.myTabHost.setCurrentTab(2); //默认显示的标签索引为2
28 }
29 }
第三种 表格标签在下方显示
就是在Tab.xml中把 LinearLayout改成RelativeLayout。 TabWidget 标签加入 android:layout_alignParentBottom="true"
1 <?xml version="1.0" encoding="utf-8"?>
2 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id ="@+id/tabhost"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical" >
7 <RelativeLayout
8 android:orientation="vertical"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 > <!-- 相对布局可以任意摆放,不像 线性布局,从上往下。 -->
12
13
14 <TabWidget
15 android:id ="@android:id/tabs"
16 android:layout_width="fill_parent"
17 android:layout_height="wrap_content"
18 android:layout_alignParentBottom="true"
19 />
20 <FrameLayout
21 android:id="@android:id/tabcontent"
22 android:layout_width="fill_parent"
23 android:layout_height="fill_parent"
24 >
25 <LinearLayout
26 android:id ="@+id/tab_edit"
27 android:layout_width="fill_parent"
28 android:layout_height="fill_parent"
29 android:orientation="vertical" >
30
31
32 <EditText
33 android:id ="@+id/edit"
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:textSize= "18px"
37 android:text = "请输入检索关键字"
38 />
39 <Button
40 android:id = "@+id/but"
41 android:layout_width="wrap_content"
42 android:layout_height="wrap_content"
43 android:text = "搜索"
44 />
45 </LinearLayout>
46 <LinearLayout
47 android:id ="@+id/tab_clock"
48 android:layout_width="fill_parent"
49 android:layout_height="fill_parent"
50 androidrientation="vertical" >
51
52
53 <AnalogClock
54 android:id ="@+id/myAnalogClock"
55 android:layout_width="wrap_content"
56 android:layout_height="wrap_content"
57
58 />
59
60 </LinearLayout>
61
62
63 <LinearLayout
64 android:id ="@+id/tab_sex"
65 android:layout_width="fill_parent"
66 android:layout_height="fill_parent"
67 android:orientation="vertical" >
68
69
70 <RadioGroup
71 android:id ="@+id/sex"
72 android:layout_width="fill_parent"
73 android:layout_height="wrap_content"
74 android:orientation="vertical"
75 >
76 <RadioButton
77 android:id ="@+id/male"
78 android:checked = "true"
79 android:text = "性别 : 男"
80 />
81 <RadioButton
82 android:id ="@+id/female"
83 android:text = "性别 : 女"
84 />
85 </RadioGroup>
86 </LinearLayout>
87 </FrameLayout>
88 </RelativeLayout>
89 </TabHost>