TabWidget 应用

来源:互联网 发布:linux oracle em 编辑:程序博客网 时间:2024/06/06 03:08

      Android手机打电话的界面运用的的是一个TabWidget(切换卡),通过多个标签切换显示不同内容,这个效果比不同Activity 之间跳转要快,所以如果大家做应用要实现不同Activity的跳转,可以考虑下TabWidget。首先要使用TabHost,用来存放多个Tab标签。可以说TabHost是一个容器。没一个Tab标签都可以有自己的布局。不废话,先看效果图:


关键代码如下:

TabHost tabHost = getTabHost();        //通过getTabHost()函数获得了Tab标签页的容器,用以承载可以点击的Tab标签和分页的界面布局。                LayoutInflater.from(this).inflate(R.layout.tab1, tabHost.getTabContentView(),true);        //通过LayoutInflater将tab1.xml文件中的布局转换为Tab标签页可以使用的View对象        LayoutInflater.from(this).inflate(R.layout.tab2, tabHost.getTabContentView(),true);        LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView(),true);                tabHost.addTab(tabHost.newTabSpec("TAB1").        setIndicator("首页",getResources().getDrawable(R.drawable.h001)).setContent(R.id.layout01));        //使用addTab()函数添加了第1个分页,tabHost.newTabSpec("TAB1"),建立的tabHost上,         //添加一个标识为TAB1的Tab分页        tabHost.addTab(tabHost.newTabSpec("TAB2").        setIndicator("音乐",getResources().getDrawable(R.drawable.group_head)).setContent(R.id.layout02));        tabHost.addTab(tabHost.newTabSpec("TAB3").        setIndicator("视频",getResources().getDrawable(R.drawable.sysmsg_superqq)).setContent(R.id.layout03));
下面是XML文档tab1.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id = "@+id/layout01"  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:orientation="vertical">  <TextView android:id="@+id/label"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个标签页" ></TextView></LinearLayout>

这里建了3个XML文档来布局,也可以根据具体情况整合到一个布局文件里。如果工程比较大,一般会分开写每个标签的布局文件。这样分工明确,易于维护

源代码地址  http://download.csdn.net/detail/diandianheshang/4026155

原创粉丝点击