您的位置:首页 > 其它

关于TabHost的详细解析

2016-08-03 20:47 253 查看
          首先,什么是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"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><!--FrameLayout的位置可以控制栏的位置 --><FrameLayoutandroid:layout_width="fill_parent"android:layout_height="0.0dip"android:layout_weight="1"android:id="@android:id/tabcontent"/><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="0"android:visibility="gone"/><FrameLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"><RadioGroupandroid: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"><RadioButtonandroid: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"/><RadioButtonandroid: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"/><RadioButtonandroid: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{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkId) {// TODO Auto-generated method stubswitch(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去错名字,则不会运行。
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: