android 选项卡TabHost的功能用法

首先定义三个xml文件,分别为l1.xml,l2.xml,l3.xml,每个选项卡的标签页的内容为一个xml文件。

其中l1.xml为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/l1">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

l2.xml为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/l2">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

</LinearLayout>

l3.xml为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/l3">

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

TabActivityDemo2.java:

package com.example.wenandroid;


import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost.TabSpec;

public class TabActivityDemo2 extends TabActivity {

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		android.widget.TabHost tab=getTabHost();
		LayoutInflater lay=LayoutInflater.from(this);
		lay.inflate(R.layout.l1, tab.getTabContentView(),true);
		lay.inflate(R.layout.l2, tab.getTabContentView(),true);
		lay.inflate(R.layout.l3, tab.getTabContentView(),true);
		
		TabSpec t1=tab.newTabSpec("t1");
		TabSpec t2=tab.newTabSpec("t2");
		TabSpec t3=tab.newTabSpec("t3");
		
		t1.setIndicator("选项卡1");
		t2.setIndicator("选项卡2");
		t3.setIndicator("选项卡3");
		
		t1.setContent(R.id.l1);
		t2.setContent(R.id.l2);
		t3.setContent(R.id.l3);
		
		tab.addTab(t1);
		tab.addTab(t2);
		tab.addTab(t3);
	}

}


效果如下图:



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值