Android中的选项卡(Tab)使用案例

来源:互联网 发布:朗文字典 mac 编辑:程序博客网 时间:2024/05/23 01:24

使用Tab组件的步骤说明:
1、在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件。
2、Activity要继承TabActivity。
3、调用TabActivity的getTabHost()方法得当TabHost对象。
4、通过TabHost创建Tab选项。
下面来看一个小例子:

<!--xml--><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TabHost         android:id="@+id/TabHost01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"></TabHost>    <TextView         android:id="@+id/suoyou"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/suoyou"/>    <TextView         android:id="@+id/yijie"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/yijie"/>    <TextView         android:id="@+id/weijie"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/weijie"/></FrameLayout>
//activity代码package com.yangzi.tab;import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.widget.TabHost;public class MainActivity extends TabActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //setContentView(R.layout.activity_main);        //得到TabHost对象        TabHost th = getTabHost();        LayoutInflater.from(this).inflate(R.layout.activity_main,th.getTabContentView(),true);        //添加选项,设置标签内容和显示内容        th.addTab(th.newTabSpec("all").setIndicator("所有通话").setContent(R.id.suoyou));        th.addTab(th.newTabSpec("ok").setIndicator("已接来电").setContent(R.id.yijie));        th.addTab(th.newTabSpec("cancel").setIndicator("未接来电").setContent(R.id.weijie));    }}

动手试试,看看效果吧!

0 0