Android选项卡实现之TabHost/TabSpec/TabWidget

Tab是什么就不用再用我来描述了。android的ui开发中,承载Tab的容器就是TabHost,每个Tab对应一个Activity,每一个Activity均会有本身的布局。

1.继承TabActivity html

2.布局文件中使用tabHost,tabWedgit和framework java

3.在activity中经过源码添加tab选项卡,每一个选项卡中显示指定activity中的内容。能够经过代码控制界面的显示效果。 android

下面仍是经过一个代码示例来讲明下吧: git

(1)建立一个新的工程TestTab吧 app

(2)将生成的MainActivity的继承类改为TabActivity,若是没自动生成mainactivity,那就本身手动建立一个吧: ide

public class MainActivity extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

(3)建立main的布局文件 布局

注意:TabHost ,TabWidget ,FrameLayout的ID必须分别为@android :id/tabhost,@android :id/tabs,@android :id/tabcontent
另外还要注意一下android:layout_width宽度和android:layout_height高度的取值,还要LinearLayout的android:orientation=”vertical”(LinearLayout默认是横向的)当你看到布局和我不同时你就要考虑一下这里是否是错了。(= =!由于我错过) flex

    这里面的TabHost就是tab区加上对应的属性页;TabWidget就是Tab按钮区,FrameLayout就是属性页区域. ui

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TabHost
        android:id="@android :id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <FrameLayout
                android:id="@android :id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
            <TabWidget
                android:id="@android :id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />
            <!--  android:background="@drawable/jdy_tab_background"-->
        </LinearLayout>
    </TabHost>
</LinearLayout>

(4)建立tab元素:TabSpec this

    这里面会涉及到对象TabSpec。该对象一般用TabHost来建立,里面包含了一个Tab元素所包含的信息须要用户来指定。下面来看看官方文档的解释吧:

A tab has a tab indicator, content, and a tag that is used to keep track of it. This builder helps choose among these options. For the tab indicator, your choices are: 1) set a label 2) set a label and an icon For the tab content, your choices are: 1) the id of a View 2) a TabHost.TabContentFactory that creates the View content. 3) an Intent that launches an Activity.
 

    一个tab一般包含了indicator(指示器)、content(tab对应展现的页面view,能够为这个view的id或者是一个intent)、tag。其中TabSpec就是用来辅助设置这些选项。

    <1> Indicator一般是设置tab的名称label和icon。

    <2> Content:能够是一个view的id,也能够经过TabContentFactory建立一个view,还能够是一个Intent组件来加载一个activity。

(5)建立两个Activity,暂且叫FirstActivity,SecActivity,并在Manifest文件中注册

public class SettingActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        TextView textview = new TextView(this);
        textview.setText("设置页面");
        setContentView(textview);
    }
}  

(6)在MainActivity中添加Tab -- 使用TabSpec

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mTabHost = getTabHost();
        TabSpec spec;
        
        spec = mTabHost.newTabSpec("新建");    
        spec.setIndicator("新建");
        spec.setContent(new Intent(this, NewTaskActivity.class));
        mTabHost.addTab(spec);
        
        spec = mTabHost.newTabSpec("设置");    
        spec.setIndicator("设置");
        spec.setContent(new Intent(this, SettingActivity.class));
        mTabHost.addTab(spec);
          
        mTabHost.setCurrentTab(0);         
    }

至此,一个简单的tab就作完了,运行下就能够看到结果。你也能够根据本身的喜爱更改下每一个tab的内容的activity样子。

(7)TabWidget说明:

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ android.widget.TabWidget

    经过上面的类继承关系就能够看出TabWidget是一个ViewGroup,便是一个线性布局的容器,可以容纳多个Tab。同时“The container object for this widget is TabHost”。它也是TabHost内的一个widget。


不过用过API4.0的小伙伴必定都会发现TabActivity已经被android弃用了。

    TabActivity has been deprecated, cause it is subclass of ActivityGroup, which also has been deprecated.

    ActityGroup has been deprecated, and instead Fragment has been introduced and suggested. As using Fragments is easier and more flexible than ActivityGroup. It also enable android components to have a homogeneous pattern.

后面,咱们决定采用Fragment实现tab形式的页面,也与时俱进下。 待续... ...
相关文章
相关标签/搜索