TabHostProject 标签的使用(方法二 配置文件)

使用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); //默认显示的标签
}

配置文件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>

}

要使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" />


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值