关于TabHost的详细解析

          首先,什么是TabHost控件。类似于微信上面,下面的几个按钮。下面展示一下简单的几个图,来表示。






类似于这种效果。下面展示LayOut里面的布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
     >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
        
        <!--FrameLayout的位置可以控制栏的位置  -->
          <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="1"
            android:id="@android:id/tabcontent"
            />
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:visibility="gone"
            />
        
        
           <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
            <RadioGroup
                android:id="@+id/main_radioGroup"
                android:layout_width="fill_parent"
                android:layout_height="60dp"
                android:layout_gravity="bottom"
                android:gravity="bottom"
                android:orientation="horizontal"
           
                android:background="#6F6F91"
                >
                <RadioButton
                    android:id="@+id/rb1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="标签1" 
                    android:button="@null"
                    android:textSize="30sp"
                    />
                <RadioButton
                    android:id="@+id/rb2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="标签2" 
                    android:button="@null"
                    android:textSize="30sp"
                    />
                <RadioButton
                    android:id="@+id/rb3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="标签3" 
                    android:button="@null"
                    android:textSize="30sp"
                    />
                </RadioGroup>
           </FrameLayout>
        </LinearLayout>

</TabHost>


下面是Activity里面的代码:
<pre name="code" class="plain">package com.example.tab;

import com.example.tab.intent.tab1;
import com.example.tab.intent.tab3;
import com.example.tab.intent.tb2;

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

public class MyTabLayOut extends TabActivity{

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.main_tab);
			
			final TabHost th = getTabHost();
			/*
			 * 三个Intent分别代表带个要实现的页面
			 * 
			 * */
			Intent tab11 = new Intent(MyTabLayOut.this,tab1.class);
			Intent tb22 = new Intent(MyTabLayOut.this,tb2.class);
			Intent tab33 = new Intent(MyTabLayOut.this,tab3.class);
			
			/*
			 * 1、newTabSpec("标签1"):里面的“标签1”是在RadioGroup里面进行识别的,
			 * 	    通过th.setCurrentTabByTag("标签1")进行识别
			 * 2、setIndicator("标签1", null):这个里面就的“标签1”是为TabHost取得名字,
			 * 	   但是在Tabweight里面设置了android:visibility="gone",这个框不显示。
			 * 		里面的null代表TabHost各个标签的背景,这里设为null,表示背景为空
			 * 3、setContent(tab11)):为每个标签加载一个页面
			 * 
			 * */
			th.addTab(th.newTabSpec("标签1").setIndicator("标签1", null).setContent(tab11));
			th.addTab(th.newTabSpec("标签2").setIndicator("标签2", null).setContent(tb22));
			th.addTab(th.newTabSpec("标签3").setIndicator("标签3", null).setContent(tab33));
			
			th.setCurrentTab(0);//默认在第一个页面
			
			RadioGroup main_radioGroup = (RadioGroup) findViewById(R.id.main_radioGroup);
			
			main_radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(RadioGroup group, int checkId) {
					// TODO Auto-generated method stub
					switch(checkId){
					case R.id.rb1:
						th.setCurrentTabByTag("标签1");//通过标签的Id来关联着TabHost里面的标签
						break;
					case R.id.rb2:
						th.setCurrentTabByTag("标签2");
						break;
					case R.id.rb3:
						th.setCurrentTabByTag("标签3");
						break;
					default :
						break;
					}
				}
			});
		}
}


 其中,上面的三张图是把LayOut里面的TabWeight里面的属性设置为了android:visibility="gone",关于visibility的用法,里面有三种属性,一种是占位置和显示(visible), 

第二种是只占位置不显示(invisible),第三种就是gone,不显示,不占位置。

接下来,我把TabWeight里面的android:visibility="gone"设置为visible,看看效果,这个请自行理解。





再布局里面还有一点要注意,设置TabHost、TabWeight、和FrameLayout的Id是一定要用@android:id/XXX这样的形式,每个有对应的系统Id,如果Id去错名字,则不会运行。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值