使用HostTab配置文件实现 较麻烦
Activity程序
public class MyTabHostDemo extends Activity {
private TabHost myTabHost = null;
private int[] layRes = new int[] { R.id.tab_edit, R.id.tab_clock,
R.id.tab_sex };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.tab);
this.myTabHost =(TabHost) super.findViewById(R.id.myTabHost); // 取得TabHost对象
this.myTabHost.setup();
for(int x=0;x<this.layRes.length;x++){
TabSpec myTab=this.myTabHost.newTabSpec("tab"+x); //"tab"+x 所需的选项卡标签(tag)
myTab.setIndicator("标签-"+x); //设置每个子标签的名字
myTab.setContent(this.layRes[x]);
this.myTabHost.addTab(myTab);
}
this.myTabHost.setCurrentTab(1); //默认显示的标签
}
}
private TabHost myTabHost = null;
private int[] layRes = new int[] { R.id.tab_edit, R.id.tab_clock,
R.id.tab_sex };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.tab);
this.myTabHost =(TabHost) super.findViewById(R.id.myTabHost); // 取得TabHost对象
this.myTabHost.setup();
for(int x=0;x<this.layRes.length;x++){
TabSpec myTab=this.myTabHost.newTabSpec("tab"+x); //"tab"+x 所需的选项卡标签(tag)
myTab.setIndicator("标签-"+x); //设置每个子标签的名字
myTab.setContent(this.layRes[x]);
this.myTabHost.addTab(myTab);
}
this.myTabHost.setCurrentTab(1); //默认显示的标签
}
配置文件Tab.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myTabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入搜索关键字。。。"
android:textSize="18px" />
<Button
android:id="@+id/myBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_clock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
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>
</FrameLayout>
</LinearLayout>
</TabHost>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myTabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入搜索关键字。。。"
android:textSize="18px" />
<Button
android:id="@+id/myBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab_clock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
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>
</FrameLayout>
</LinearLayout>
</TabHost>
}
要使tab标签放在下方,只需要改两个地方,线性布局改成相对布局,TabWidgt中与容器底部对齐
<RelativeLayout
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"
android:layout_alignParentBottom="true" />
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"
android:layout_alignParentBottom="true" />