TabHost三种方法

TabHost三种方法_第1张图片

方式一:TabHost继承TabActivity方法相当简单

首先看布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 第一个布局 -->

    <LinearLayout
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab1" />
    </LinearLayout>

    <!-- 第二个布局 -->

    <LinearLayout
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab2" />
    </LinearLayout>

    <!-- 第三个布局 -->

    <LinearLayout
        android:id="@+id/view3"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tab3" />
    </LinearLayout>

</FrameLayout>

然后就是一个activity就行了MainActivity

package com.example.tabhost_trynew;

import com.example.tabhost_trynew.R;

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

public class MainActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//TabHost extends FrameLayout
		TabHost tabHost = getTabHost();
		////把我们的布局文件添加到tabHost的FrameLayout下面 
		LayoutInflater.from(this).inflate(R.layout.activity_main,
				tabHost.getTabContentView(), true);
		
		/**
		 * public void addTab(TabSpec tabSpec)
		 * tabSpec:A tab has a tab indicator, content, and a tag 
		 * 
		 */
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.view1));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2")
				.setContent(R.id.view2));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
				.setContent(R.id.view3));
		
		
		 //setOnTabChangedListener
        tabHost.setOnTabChangedListener(new OnTabChangeListener(){  
            @Override
            public void onTabChanged(String tabId) {
            	if (tabId.equals("tab1")) {   
                }
                if (tabId.equals("tab2")) {   
                }
                if (tabId.equals("tab3")) {  
                }
            }            
        }); 
        
    	
	}
}

++++++++++++++++++++++++++++++++++++++++++方式二+++++++++++++++++++++++++++++++++++

方式二:TabHost继承Activity(xml布局实现)

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <!-- TabHost是盛放Tab的容器。TabHost必须包含一个 TabWidget和一个FrameLayout -->
     <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

         <!-- 这里添加一个布局,原因是 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <!-- TabWidget用来显示选项卡 -->
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <!-- 用来显示选项卡对应的内容 -->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <!-- 第一个tab的布局 -->
                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="林炳东" />

                </LinearLayout>

                <!-- 第二个tab的布局 -->
                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="张小媛" />

                </LinearLayout>

                <!-- 第三个tab的布局 -->
                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="马贝贝" />

                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost> 
    
</LinearLayout>
MainActivity

package com.example.tabhost_try2;

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

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		TabHost th=(TabHost)findViewById(R.id.tabhost);
		// 作用是来初始化我们的TabHost容器:
		th.setup();            
		
		th.addTab(th.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
		th.addTab(th.newTabSpec("tab2").setIndicator("tab2",null).setContent(R.id.tab2));
		th.addTab(th.newTabSpec("tab3").setIndicator("tab3",null).setContent(R.id.tab3));

	}
}

+++++++++++++++++++++++++++++++++++++方式三+++++++++++++++++++++++++++++++++++++++++

拆分方法(跟方法二类似)

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@+id/tabhost"         
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <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" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

tab1.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/LinearLayout01" 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <TextView 
            android:text="我是标签1的内容喔"
            android:id="@+id/TextView01" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
       </TextView>
 </LinearLayout>

tab2.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView android:text="标签2"
                  android:id="@+id/TextView01" 
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
</LinearLayout>

MainActivity

package com.example.tabhost_try3;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.TabHost;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TabHost m = (TabHost) findViewById(R.id.tabhost);
		m.setup();

		LayoutInflater i = LayoutInflater.from(this);
		i.inflate(R.layout.tab1, m.getTabContentView());
		i.inflate(R.layout.tab2, m.getTabContentView());

		m.addTab(m.newTabSpec("tab1").setIndicator("tab1")
				.setContent(R.id.LinearLayout01));
		m.addTab(m.newTabSpec("tab2").setIndicator("tab2")
				.setContent(R.id.LinearLayout02));

	}
}


你可能感兴趣的:(xml,tabhost)