选项卡的创建有几种方式呢?

第一种,继承TabActivity,TabActivity类似于ListActivity,这样我们可以不用指定xml布局文件,系统会提供默认的xml布局。说明一点,我们也可以自己配置xml布局文件(但是不能改变TabHost和TabWidget,TabContent的id名称),将选项卡放置在屏幕的下方。

@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TabHost tabHost = getTabHost();
       //创建一个容器
       TabHost.TabSpec tabSpec = tabHost.newTabSpec("FIRST");//picture是这个标签的唯一标识
       //设置这个标签的内容
      tabSpec.setContent(this);
       //设置这个标签的lable,也就是这个标签的选项卡上显示的文字
       tabSpec.setIndicator("FIRST");
       //将这个标签添加到标签容器中
       tabHost.addTab(tabSpec);
       TabHost.TabSpec tabSpceSecond = tabHost.newTabSpec("SECOND");
       tabSpceSecond.setContent(this);
       tabSpceSecond.setIndicator("SECOND");
       tabHost.addTab(tabSpceSecond);
       tabHost.setCurrentTab(0);
      
   }

 

需要另外注意的一点:

tabSpec.setContent(***);方法必须要调用,否则会报异常。setContent方法一共有三个重载,

   //这个主要用于自己配置xml布局,并且在FrameLayout里面配置了view,作为TabHost的显示内容

    public TabSpec setContent(int viewId) {
        mContentStrategy = new ViewIdContentStrategy(viewId);
        return this;
    }

  //这种主要适用于继承TabActivity,并且使用系统自带的xml布局文件

    public TabSpec setContent(TabContentFactory contentFactory) {
        mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
        return this;
    }

//  //这种主要适用于继承TabActivity,并且使用系统自带的xml布局文件
    public TabSpec setContent(Intent intent) {
        mContentStrategy = new IntentContentStrategy(mTag, intent);
        return this;
    }

提个小问题,我们知道,在TabHost中用于存储显示view的控件时FrameLayout,对于帧布局来说,

<FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:padding="5dp" android:layout_weight="1">
            <TextView android:id="@+id/first" android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:text="fist" />
            <TextView android:id="@+id/second" android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:text="second" />
</FrameLayout>

第二个TextView会将第一个TextView覆盖,那么TabHost又是如何实现点击不同的选项卡显示不同的view的呢?

  1. public TabSpec setContent(int viewId) {
        mContentStrategy = new ViewIdContentStrategy(viewId);
        return this;
    }

ViewIdContentStrategy:这个类的作用是创建显示View,并将其设为gone,

2. private ViewIdContentStrategy(int viewId) {
         mView = mTabContent.findViewById(viewId);
         if (mView != null) {
             mView.setVisibility(View.GONE);
         } else {
             throw new RuntimeException("Could not create tab content because " +
                     "could not find view with id " + viewId);
         }
     }

3. 在setCurrentTab方法中:

mCurrentView = spec.mContentStrategy.getContentView();

在getContentView方法中,又将这个view设为可见,即可显示在界面上。

public View getContentView() {
         mView.setVisibility(View.VISIBLE);
         return mView;
     }

第二种,继承Activity,使用自己配置的xml布局文件,这一种不推荐使用,因为他只是将TabActivity的工作重新做了一遍。

posted on 2011-01-20 18:19  Android火鸟  阅读(4438)  评论(0编辑  收藏  举报