Android基础:十八般武器之TabHost

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
		 android:id="@android:id/tabhost"
		 android:layout_width="fill_parent"
		 android:layout_height="fill_parent"
>
<LinearLayout
		 android:orientation="vertical"
		 android:layout_width="fill_parent"
		 android:layout_height="fill_parent" 
		 >
<FrameLayout
		 android:id="@android:id/tabcontent"
		 android:layout_width="fill_parent"
		 android:layout_height="fill_parent"
		 android:layout_weight="1"
/>
<TabWidget 
		 android:id="@android:id/tabs"
		 android:layout_width="fill_parent"
		 android:layout_height="wrap_content"
		 android:layout_weight="0"
/>
</LinearLayout>
</TabHost>

tab1.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是tab1" />

</LinearLayout>

tab2.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这里是tab2" />

</LinearLayout>

TabHostActivity.java

package com.edu.tabhost;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class TabHostActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tabInit();
    }
    public void tabInit(){
    	TabHost th = this.getTabHost();//实例化TabHost
    	//第一个标签
    	Intent i1 = new Intent();
    	i1.setClass(this, tab1activity.class);
    	TabHost.TabSpec ts1 = th.newTabSpec("tab1");//创建标签项
    	ts1.setIndicator("标签1");//设置指示器文本
    	ts1.setContent(i1);//填充布局
    	th.addTab(ts1);//填加到TabHost
    	//第二个标签
    	Intent i2 = new Intent();
    	i2.setClass(this, tab2activity.class);
    	TabHost.TabSpec ts2 = th.newTabSpec("tab2");//创建标签项
    	ts2.setIndicator("标签2");//设置指示器文本
    	ts2.setContent(i2);//填充布局
    	th.addTab(ts2);//填加到TabHost
    	//默认标签 
    	th.setCurrentTab(0);
    }
}

tab1activity.java

package com.edu.tabhost;

import android.app.Activity;
import android.os.Bundle;

public class tab1activity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.tab1);
		super.onCreate(savedInstanceState);
	}
}
tab2activity.java

package com.edu.tabhost;

import android.app.Activity;
import android.os.Bundle;

public class tab2activity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.tab2);
		super.onCreate(savedInstanceState);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值