android TabHost选项卡示例

1. 继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布局文件中定义即可。

tabactivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <LinearLayout android:id="@+id/firstTab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        
        <TextView android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="这是第一个选项卡"
            android:textSize="20px"/>
        
    </LinearLayout>
    
    <LinearLayout android:id="@+id/secondTab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        
        <TextView android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="这是第二个选项卡"
            android:textSize="20px"/>
        
    </LinearLayout>
    
     <LinearLayout android:id="@+id/thirdTab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        
        <TextView android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="这是第三个选项卡"
            android:textSize="20px"/>
        
    </LinearLayout>

</FrameLayout>

2. 不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的ID必须是 @android :id/tabs,FrameLayout的ID必须是 @android :id/tabcontent,TabHost的ID可自定义。

tabxml.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="使用布局文件中定义TabHost的方式实现TabHost"
        android:textSize="15px"/>
    <TabHost android:id="@+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">
            
            <TabWidget android:id="@android:id/tabs"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </TabWidget>
            <FrameLayout android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <TextView android:id="@+id/view1"
                    android:layout_width="fill_parent"
               		android:layout_height="fill_parent"
               		android:text="这是第一个选项卡"
               		android:textSize="20px"/>
                <TextView android:id="@+id/view2"
                    android:layout_width="fill_parent"
               		android:layout_height="fill_parent"
               		android:text="这是第二个选项卡"
               		android:textSize="20px"/>
                <TextView android:id="@+id/view3"
                    android:layout_width="fill_parent"
               		android:layout_height="fill_parent"
               		android:text="这是第三个选项卡"
               		android:textSize="20px"/>
                
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

MyTabActivity.java 文件:

package com.example.baseexample;

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

public class MyTabActivity extends TabActivity {
	private TabHost myTabHost ;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		myTabHost = this.getTabHost();
		LayoutInflater.from(this).inflate(R.layout.tabactivity, myTabHost.getTabContentView(),true);
		myTabHost.addTab(myTabHost.newTabSpec("选项卡1").setIndicator("选项卡1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.firstTab));
		myTabHost.addTab(myTabHost.newTabSpec("选项卡2").setIndicator("选项卡2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.secondTab));
		myTabHost.addTab(myTabHost.newTabSpec("选项卡3").setIndicator("选项卡3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.thirdTab));
	}
}

TabXmlActivity.java文件:


package com.example.baseexample;

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

public class TabXmlActivity extends Activity {
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabxml);
		TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
		tabHost.setup();
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view1));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view2));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view3));
	}
}




你可能感兴趣的:(android TabHost选项卡示例)