android选项卡TabHost功能和用法详解

选项卡TabHost介绍
TabHost可以方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器大小相同的组件摆放区域
TabHost是一个简单的容器,提供如下两种方法来创建选项卡
newTabSpec(String tag):创建选项卡
addTab(TabHost.TabSpec tabSpec):添加选项卡

tab选项卡效果图:

标题插入图片时,需要在AndroidManifest.xml设置一下主题,默认主题,图片显示不出来,可以设置自己喜欢的主题

activity_main.xml页面中定义了四个tab选项卡,里边随便放了一下组件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TabHost
        android:id="@android: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" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    
                    <Button 
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="button"/>
                    
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    
                    <EditText 
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="edittext"/>
                    
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    
                    <TextView 
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="textview"/>
                    
                </LinearLayout>
                
                <LinearLayout
                    android:id="@+id/tab4"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    
                    <EditText 
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="edittext"/>
                    
                    <TextView 
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="textview"/>
                    
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

MainActivity.java(大部分都有注释)

package com.example.tab;

import android.app.Activity;
import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private TabHost tabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //获取该Activity里面的TabHost组件
        tabs = (TabHost) findViewById(android.R.id.tabhost);
        //初始化TabHost容器
        tabs.setup();
        
        //资源对象
        Resources res = getResources();
        
        TabHost.TabSpec spec;
        //创建第一个Tab页
        spec = tabs.newTabSpec("tab1")
        		.setIndicator("tab1")	//设置标题
        		.setContent(R.id.tab1);	//设置内容
        //添加第一个标签页
        tabs.addTab(spec);
        
        //res.getDrawable(R.drawable.wo)添加图片
        spec = tabs.newTabSpec("tab2").setIndicator("tab2", res.getDrawable(R.drawable.wo)).setContent(R.id.tab2);
        tabs.addTab(spec);
        spec = tabs.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.tab3);
        tabs.addTab(spec);
        spec = tabs.newTabSpec("tab4").setIndicator("tab4").setContent(R.id.tab4);
        tabs.addTab(spec);
        
        //设置默认选项卡,第几个,从0开始
        tabs.setCurrentTab(1);
        
        //标签切换事件处理,setOnTabChangedListener
        tabs.setOnTabChangedListener(new OnTabChangeListener(){

        	//tabId是newTabSpec参数设置的tab页名,
			@Override
			public void onTabChanged(String tabId) {
				if (tabId.equals("tab1")) {   //第一个标签  
	               Toast.makeText(MainActivity.this, "点击tab1", Toast.LENGTH_SHORT).show();
	           }  
	           if (tabId.equals("tab2")) {   //第二个标签  
	               Toast.makeText(MainActivity.this, "点击tab2", Toast.LENGTH_SHORT).show();
	           }  
	           if (tabId.equals("tab3")) {   //第三个标签  
	               Toast.makeText(MainActivity.this, "点击tab3", Toast.LENGTH_SHORT).show();
	           } 
	           if (tabId.equals("tab4")) {   //第四个标签  
	               Toast.makeText(MainActivity.this, "点击tab4", Toast.LENGTH_SHORT).show();
	           }
			}
        	
        });
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值