关于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去错名字,则不会运行。
相关文章推荐
- 1810: water problem II(今天心情不爽,map)
- 单张图片上传表单提交
- cocos2dx3.x绘制自己的node
- c#.net 是如何访问 SQL Server 数据库
- 我自己的网站--搭建环境(二)
- Windows 7您的凭据不工作
- Part3_lesson3---ARM伪指令学习
- 一,VS2013下cocos2d的环境搭建以及工程创建
- 数据结构实验之串二:字符串匹配
- C++ STL 一般总结
- 擅长排列的小明
- hdu 1233 还是畅通工程
- #8.VNC服务器的搭建
- 双向队列
- ThinkPHP入门(二)
- HDU 1872 稳定排序
- 【zzuliOJ】1896 - 985的买饮料难题(贪心)
- stm32f4 dsp库arm_cfft_f32 fft用法
- ztree 懒加载
- ORACLE关于分组排序函数用法