zoukankan      html  css  js  c++  java
  • TabHost理解与使用

    一.继承关系

    java.lang.Object  
       ↳ 	android.view.View  
      	   ↳ 	android.view.ViewGroup  
      	  	   ↳ 	android.widget.FrameLayout  
      	  	  	   ↳ 	android.widget.TabHost

     

    二.概述

    TAB的容器。这个对象包含两个子元素:

    1. TabWidget:管理标签(tabs),用户点击来选择一个特定的标签,是它告诉TabHost去切换界面的
    2. FrameLayout:对象显示该页的内容

     

    三.常用方法

    1. public void addTab(TabHost.TabSpec tabSpec)
    2. public void setup ():在addTab之前要先调用setup

    四.三个内部类

    1. class:TabHost.TabSpec
    2. interface:TabHost.OnTabChangeLisetener
    3. interface:TabHost.TabContentFactory

    TabHost.TabSpec

    tab(标签)有一个indicator,content.例如:

    tabHost.addTab(tabHost.newTabSpec("tab_time").setIndicator("时钟").setContent(R.id.tab_time));

    1.indicator

    有三个重载的方法可以设置标签的名字和图案。返回值都是TabHost.TabSpec

    1. setIndicator(CharSequence label)
    2. setIndicator(View view)
    3. setIndicator(CharSequence lable,Drawable icon)

    2.content

    返回值都是TabHost.TabSpe。是第一个比较常用。

    1. **setContent(int viewId)**传入视图的ID与之关联起来
    2. setContet(Intent intent)在TabHost.TabContentFactory创建的这个视图的内容
    3. setContent((TabHost.TabContentFactory contentFactory)

    3.tag

    这是相当于一个tag的身份证,在 new TabSpec(String tag)决定了

     

    五.例子

    http://www.cnblogs.com/Mihai/

     

    六.源码大观

    public class TabHost...{
        //常用属性
        private TabWidget mTabWidget;
        private FrameLayout mTabContent;
        private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2);
        private OnKeyListener mTabKeyListener;
        public void setup(){
            //这里实例化TabWiget
            mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
            if (mTabWidget == null) {
                throw new RuntimeException(
                        "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
            }
            ....
            mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
                public void onTabSelectionChanged(int tabIndex, boolean clicked) {
                    setCurrentTab(tabIndex);
                    if (clicked) {
                        mTabContent.requestFocus(View.FOCUS_FORWARD);
                    }
                }
            });
    
            mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
            if (mTabContent == null) {
                throw new RuntimeException(
                        "Your TabHost must have a FrameLayout whose id attribute is "
                                + "'android.R.id.tabcontent'");
            }
        }
    }

    注意:在自定义自己的TabHost的时候,Tabwiget和FrameLayout不可以自定义Id。为它需要在setup里面实例化,因此需要在addTab添加内容之前调用setup方法

  • 相关阅读:
    To select the file to upload we can use the standard HTML input control of type
    Cascading Menu Script using Javascript Explained
    网站首页head区代码规范
    轻松掌握 Java 泛型
    JDK 5.0 中的泛型类型学习
    如何在firefox下获取下列框选中option的text
    是同步方法还是 synchronized 代码? 详解多线程同步规则
    javascript select option对象总结
    Select的动态取值(Text,value),添加,删除。兼容IE,FireFox
    javascript在ie和firefox下的一些差异
  • 原文地址:https://www.cnblogs.com/Mihai/p/5598554.html
Copyright © 2011-2022 走看看