你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中选项卡TabHost的基本使用

[日期:2011-12-11] 来源:Linux社区  作者:jiangqq781931404 [字体:大 中 小]

然后使用第二种方法创建TabHost

主Activity类:

  1. package com.jiangqq.tabhost;  
  2.   
  3. import Android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TabHost;  
  6. import android.widget.TabHost.TabSpec;  
  7.   
  8. /** 
  9.  * 本例是实现TabHost----->直接在XML文件中进行配置 【注意】在xml文件中 
  10.  * TahWidget和FrameLayout标签中的ID,必须要使用Android中默认的 
  11.  *  
  12.  * @author jiangqq 
  13.  *  
  14.  */  
  15. public class TabHostActivity_First extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.tabhost_first);  
  21.         // 获取TabHost   
  22.         TabHost tabHost = (TabHost) findViewById(R.id.tabs);  
  23.         tabHost.setup();  
  24.         // 设置选项卡   
  25.         // 参数:是选项卡的标签   
  26.         TabSpec parentSpec = tabHost.newTabSpec("parent");  
  27.         parentSpec.setIndicator("基类",  
  28.                 this.getResources().getDrawable(R.drawable.announcements256));  
  29.         parentSpec.setContent(R.id.tab_1);  
  30.   
  31.         TabSpec subSpec = tabHost.newTabSpec("sub");  
  32.         subSpec.setIndicator("子类",  
  33.                 this.getResources().getDrawable(R.drawable.content256));  
  34.         subSpec.setContent(R.id.tab_2);  
  35.   
  36.         tabHost.addTab(parentSpec);  
  37.         tabHost.addTab(subSpec);  
  38.   
  39.     }  
  40. }  

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TabHost  
  8.         android:id="@+id/tabs"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" >  
  11.   
  12.         <LinearLayout  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="fill_parent"  
  15.             android:orientation="vertical" >  
  16.   
  17.             <TabWidget  
  18.                 android:id="@android:id/tabs"  
  19.                 android:layout_width="fill_parent"  
  20.                 android:layout_height="wrap_content" >  
  21.             </TabWidget>  
  22.   
  23.             <FrameLayout  
  24.                 android:id="@android:id/tabcontent"  
  25.                 android:layout_width="fill_parent"  
  26.                 android:layout_height="fill_parent" >  
  27.   
  28.                 <TextView  
  29.                     android:id="@+id/tab_1"  
  30.                     android:layout_width="fill_parent"  
  31.                     android:layout_height="fill_parent"  
  32.                     android:text="jiangqq_TabHostDemo_First" >  
  33.                 </TextView>  
  34.   
  35.                 <TextView  
  36.                     android:id="@+id/tab_2"  
  37.                     android:layout_width="fill_parent"  
  38.                     android:layout_height="fill_parent"  
  39.                     android:text="jiangqq_TabHostDemo_Second" >  
  40.                 </TextView>  
  41.             </FrameLayout>  
  42.         </LinearLayout>  
  43.     </TabHost>  
  44.   
  45. </LinearLayout>  

大家可以比较一下两种方法的异同点,差异不是很大,创建起来也比较简单

linux
相关资讯       Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款