Android中的选项卡是用TabHost实现的,下面我们用一个例子说明他的使用方法

首先,定义TabHost的布局文件:


代码如下:

<?xml version="1.0" encoding="utf-8"?>

   android:id="@android:id/tabhost" android:layout_width="fill_parent"
   android:layout_height="fill_parent">

           android:layout_width="fill_parent" android:layout_height="fill_parent">

                   android:layout_alignParentBottom="true"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"/>

                   android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" />

   



其中,TabWidget即是选项卡上面的标签,FrameLayout是选项卡的内容。
在Java类文件中定义如下:



public class MainActivity extends TabActivity {

   private TabHost my_tabhost;  
   private TabWidget my_tabwidget;
   private int i,k;
   private TextView tv;

   private String[] tabMenu = { "系统", "硬件", "操作"};
   private Intent intent0, intent1, intent2;
   private Intent[] intents = { intent0, intent1, intent2};
   private TabHost.TabSpec tabSpec0, tabSpec1, tabSpec2, tabSpec3;
   private TabHost.TabSpec[] tabSpecs = { tabSpec0, tabSpec1, tabSpec2, tabSpec3};

   public static Context mContext;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
               // 不要窗体标题
              requestWindowFeature(Window.FEATURE_NO_TITLE);
              setContentView(R.layout.activity_main);
       setContentView(R.layout.activity_main);

       my_tabhost = getTabHost();

       intent0 = new Intent(this, system.class);
       intent1 = new Intent(this, hardware.class);
       intent2 = new Intent(this, operation.class);

       tabSpec0 = my_tabhost.newTabSpec("system").setIndicator(tabMenu[0],null).
               setContent(intent0);
       tabSpec1 = my_tabhost.newTabSpec("hardware").setIndicator(tabMenu[1],null).
               setContent(intent1);
       tabSpec2 = my_tabhost.newTabSpec("operation").setIndicator(tabMenu[2],null).
               setContent(intent2);

       my_tabhost.addTab(tabSpec1);
       my_tabhost.addTab(tabSpec0);
       my_tabhost.addTab(tabSpec2);

                // 设置默认选中的选项卡为第2个      
                    my_tabhost.setCurrentTab(1);

   }

}


每一个选项卡对应一个Intent,每一个Intent又对应一个类,选中这个选项卡时,就显示对应的类。
运行结果如下:


更多相关文章

  1. 2011.06.29(2)——— android 完全退出
  2. IDEA 快捷键 Android(安卓)Studio快捷键
  3. Ubuntu16.04配置ADB调试环境
  4. Android(安卓)Studio修改默认Activity继承AppCompatActivity
  5. 2011.06.29(2)——— android 完全退出
  6. Ubuntu10.04 LTS 下编译Android
  7. android中菜单以及自定义组件的使用
  8. 特效--------圆角 倒影
  9. Android(安卓)NDK: WARNING: APP_PLATFORM Android-19 Is Larger

随机推荐

  1. Android——启动过程详解
  2. Android 系统HAL 简介
  3. [Android--Tool]Android如何将他人的代码
  4. android 笔记 ---- 使用Hessian与Java服
  5. Android样式的开发:shape篇
  6. Android(安卓)multidex 使用 与 实现原理
  7. [置顶] 【电子书下载】《Android应用程序
  8. Android 中的Parcelable序列化对象
  9. Android SDK 2.3与Eclipse最新版开发环境
  10. Android权限安全(5)组件的android:export