标签布局Tab与TabHost详细教程

上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助。今天在这里和大家一起学习一下标签布局

 

    
1.建新一个Tab项目(意注:不要生成main Acitivyt)

    2.在包里头建新一个类(TabDemoActivity,承继于TabActivity而不是Activity)

    package com.example.chong;

     

    import android.app.TabActivity;

    public class TabDemoActivity extends TabActivity

    {

              protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
}

    }

    3、为标签页面计划对应页面布局,一般用采FrameLayout作为根布局,个每标签页面对应一个子节点的layout。

     

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="
http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

        <!-- 第一个Tab对应的布局 -->

        <TextView
        android:id="@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/blue"
        android:text="这里是Tab1里的容内" />
    <!-- 第er个Tab对应的布局 -->
    <TextView
        android:id="@+id/view2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/blue"
        android:text="这里是Tab2里的容内" />
    <!-- 第三个Tab对应的布局 -->
    <TextView
        android:id="@+id/view3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/blue"
        android:text="这里是Tab3里的容内" />

    </FrameLayout>

    4.在TabDemoActivity中,首先该应明声TabHost,然后用LayoutInflater过滤出布局,给TabHost加上含有Tab页面的FrameLayout

    private TabHost tabHost;

    TabHost = this.getTabHost();//从TabActivity下面获得放置Tab的TabHost

    LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(),true);

    // from(this)从这个TabActivity获得LayoutInflater

    // R.layout.main放存Tab布局

    // 否是将inflate全系到根布局元素上

    5.在TabHost中建创一个标签,然后设置它的标题、图标、标签页布局

    tabHost

                 .addTab(tabHost.newTabSpec("tab1") //建创一个新的标签“tab1”

                 .setIndicator("KK",getResources().getDrawable(R.drawable.jpg)) // 设置表现标题KK,设置标签图标为jpg

                 .setContent(R.id.view1)); // 设置该标签页的布局容内为R.idview对应的容内

    6.可认为标签 tabHost 添加标签切换事件处理(setOnTabChangedListener) 

    每日一道理
爱,有的时候不需要山盟海誓的承诺,但她一定需要细致入微的关怀与问候;爱,有的时候不需要梁祝化蝶的悲壮,但她一定需要心有灵犀的默契与投合;爱,有的时候不需要雄飞雌从的追随,但她一定需要相濡以沫的支持与理解。

    tabHost.setOnTabChangedListener ( new OnTabChangedListener () {

                @Override

               public void onTabChanged(String tabId)

              {

                     //TODO Auto-generated method stub

              }

    });

     

    main.xml

     

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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:background="@drawable/blue"
        android:text="这里是Tab1里的容内" />
    
    <TextView
        android:id="@+id/view2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/blue"
        android:text="这里是Tab2里的容内" />
    
    <TextView
        android:id="@+id/view3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/blue"
        android:text="这里是Tab3里的容内" />

</FrameLayout>

    
TabDemoActivity.java

package com.example.chong;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class TabDemoActivity extends TabActivity implements OnTabChangeListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("TabDemoActivity");
        TabHost tabHost = getTabHost();
        LayoutInflater.from(this).inflate(R.layout.tab_demo,tabHost.getTabContentView(),true);
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab11").setContent(R.id.view1));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab22").setContent(R.id.view2));
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab33").setContent(R.id.view3));
        tabHost.setOnTabChangedListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tab_demo, menu);
        return true;
    }

	@Override
	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		if(tabId.equals("tab1"))
		{
			Toast.makeText(this, "进入了第一个标签页对应的页面!", Toast.LENGTH_SHORT).show();  
		}
		if(tabId.equals("tab2"))
		{
			Toast.makeText(this, "进入了第二个标签页对应的页面!", Toast.LENGTH_SHORT).show();  
		}
		if(tabId.equals("tab3"))
		{
			Toast.makeText(this, "进入了第三个标签页对应的页面!", Toast.LENGTH_SHORT).show();  
		}
	}
    
}

    
 

文章结束给大家分享下程序员的一些笑话语录: 小沈阳版程序员~~~ \n程序员其实可痛苦的了......需求一做一改,一个月就过去了;嚎~ \n需求再一改一调,一季度就过去了;嚎~ \n程序员最痛苦的事儿是啥,知道不?就是,程序没做完,需求又改了; \n程序员最最痛苦的事儿是啥,知道不? 就是,系统好不容易做完了,方案全改了; \n程序员最最最痛苦的事儿是啥,知道不? 就是,系统做完了,狗日的客户跑了; \n程序员最最最最最痛苦的事儿是啥,知道不? 就是,狗日的客户又回来了,程序给删没了!

posted @ 2013-05-08 19:46  xinyuyuanm  阅读(341)  评论(0编辑  收藏  举报