UI组件:选项卡(TabHost)

来源:互联网 发布:淘宝落地窗帘布 编辑:程序博客网 时间:2024/05/16 10:02

与TabHost结合使用的还有如下组件:

  • TabWidget : 代表选项卡的标签条。
  • TabSpec : 代表选项卡的一个Tab页面。

使用TabHost的一般步骤如下:

  1. 在界面布局文件中定义TabHost组件,并为该组件定义该选项卡的内容。
  2. Activity应该继承TabActivity。
  3. 调用TabActivity的getTabHost()方法获取TabHost对象。
  4. 通过TabHost对象的方法来创建、添加选项卡。

TabHost只是一个容器,它提供了如下两个方法来创建、添加选项卡:

  • newTabSpec(String tab) : 创建选项卡。
  • addTab(TabHost.TabSpec tabSpec) : 添加选项卡。

除此之外,TabHost还提供了一些方法来获取当前选项卡,获取当前View的方法,具体可以参考API文档。
下面的实例实现的通话记录界面。
界面效果如下图所示:
这里写图片描述
布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <!--TabWidget定义选项卡的标题条-->        <TabWidget            android:id="@android:id/tabs"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_margin="10dp">            <!--定义第一个标签页的内容-->            <LinearLayout                android:id="@+id/tab1"                android:orientation="vertical"                android:layout_width="match_parent"                android:layout_height="match_parent">                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:text="tab1"/>            </LinearLayout>            <!--定义第二个标签页的内容-->            <LinearLayout                android:id="@+id/tab2"                android:orientation="vertical"                android:layout_width="match_parent"                android:layout_height="match_parent">                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:text="tab2"/>            </LinearLayout>            <!--定义第三个标签页的内容-->            <LinearLayout                android:id="@+id/tab3"                android:orientation="vertical"                android:layout_width="match_parent"                android:layout_height="match_parent">                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:text="tab3"/>            </LinearLayout>        </FrameLayout>    </LinearLayout></TabHost>

主程序代码如下所示:

public class TabHostActivity extends TabActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_tab_host);        TabHost tabHost = getTabHost();        //创建第一个Tab页        TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1")                .setIndicator("已接电话")       //设置标题                .setContent(R.id.tab1);         //设置内容        //添加第一个标签页        tabHost.addTab(tab1);        TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2")                .setIndicator("呼出电话")                .setContent(R.id.tab2);        //添加第二个标签页        tabHost.addTab(tab2);        TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3")                .setIndicator("未接电话")                .setContent(R.id.tab3);        //添加第三个标签页        tabHost.addTab(tab3);    }}
0 0
原创粉丝点击