最近在解决TabActivity过期的问题时,发现Android中选项卡有几种实现方法:继承TabActivity,继承ActivityGroup,直接继承Activity和继承FragmentActivity。其中TabActivity在API 13(Android 3.2)被标记为过期,ActivityGroup在API 14(Android 4.0)被标记为过期,目前google推荐使用的是Fragment,也就是继承FragmentActivity。虽然TabActivity和ActivityGroup被标记为过期,已经不推荐使用,但在要求不是很高的时候用起来还是比使用Fragment要方便。

使用TabActivity实现选项卡可以不需要定义布局文件,实现案例如下:

[java]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package yuchu.appmanager; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.Window; import android.widget.TabHost; @SuppressWarnings ( "deprecation" ) public class MainTabActivity extends TabActivity { private Intent mAIntent; private Intent mBIntent; public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .requestWindowFeature(Window.FEATURE_NO_TITLE); this .mAIntent = new Intent( this , ShowAppGrid. class ); this .mBIntent = new Intent( this , ShowRunApps. class ); TabHost tabhost = getTabHost(); tabhost.addTab(tabhost.newTabSpec( "tab1" ).setIndicator( "所有资源" ).setContent( this .mAIntent)); tabhost.addTab(tabhost.newTabSpec( "tab2" ).setIndicator( "正在运行" ).setContent( this .mBIntent)); } } package yuchu.appmanager; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.Window; import android.widget.TabHost; @SuppressWarnings ( "deprecation" ) public class MainTabActivity extends TabActivity { private Intent mAIntent; private Intent mBIntent; public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .requestWindowFeature(Window.FEATURE_NO_TITLE); this .mAIntent = new Intent( this , ShowAppGrid. class ); this .mBIntent = new Intent( this , ShowRunApps. class ); TabHost tabhost = getTabHost(); tabhost.addTab(tabhost.newTabSpec( "tab1" ).setIndicator( "所有资源" ).setContent( this .mAIntent)); tabhost.addTab(tabhost.newTabSpec( "tab2" ).setIndicator( "正在运行" ).setContent( this .mBIntent)); } }

使用ActivityGroup实现选项卡也相当方便,布局文件如下:

[html]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <? 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 = "fill_parent" android:layout_height = "fill_parent" > < LinearLayout android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TabWidget android:id = "@android:id/tabs" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < FrameLayout android:id = "@android:id/tabcontent" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > </ FrameLayout > </ LinearLayout > </ TabHost > <? 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 = "fill_parent" android:layout_height = "fill_parent" > < LinearLayout android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TabWidget android:id = "@android:id/tabs" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < FrameLayout android:id = "@android:id/tabcontent" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > </ FrameLayout > </ LinearLayout >

</TabHost> 代码如下:

[java]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 package yuchu.appmanager; import android.app.ActivityGroup; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Window; import android.widget.TabHost; @SuppressWarnings ( "deprecation" ) public class MainTabActivity extends ActivityGroup { private TabHost tabHost; private Intent mAIntent; private Intent mBIntent; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.maintabs); this .mAIntent = new Intent( this , ShowAppGrid. class ); this .mBIntent = new Intent( this , ShowRunApps. class ); tabHost=(TabHost)findViewById(android.R.id.tabhost); tabHost.setup(); tabHost.setup( this .getLocalActivityManager()); LayoutInflater inflater = LayoutInflater.from( this ); inflater.inflate(R.layout.showgrid, tabHost.getTabContentView()); inflater.inflate(R.layout.showrunning, tabHost.getTabContentView()); tabHost.addTab(tabHost.newTabSpec( "tab1" ).setIndicator( "所有资源" ).setContent( this .mAIntent)); tabHost.addTab(tabHost.newTabSpec( "tab2" ).setIndicator( "正在运行" ).setContent( this .mBIntent)); } } package yuchu.appmanager; import android.app.ActivityGroup; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Window; import android.widget.TabHost; @SuppressWarnings ( "deprecation" ) public class MainTabActivity extends ActivityGroup { private TabHost tabHost; private Intent mAIntent; private Intent mBIntent; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.maintabs); this .mAIntent = new Intent( this , ShowAppGrid. class ); this .mBIntent = new Intent( this , ShowRunApps. class ); tabHost=(TabHost)findViewById(android.R.id.tabhost); tabHost.setup(); tabHost.setup( this .getLocalActivityManager()); LayoutInflater inflater = LayoutInflater.from( this ); inflater.inflate(R.layout.showgrid, tabHost.getTabContentView()); inflater.inflate(R.layout.showrunning, tabHost.getTabContentView()); tabHost.addTab(tabHost.newTabSpec( "tab1" ).setIndicator( "所有资源" ).setContent( this .mAIntent)); tabHost.addTab(tabHost.newTabSpec( "tab2" ).setIndicator( "正在运行" ).setContent( this .mBIntent)); } }

更多相关文章

  1. 重温android studio jni编译生成so文件
  2. Android 笔记:读取配置文件config.properties
  3. Android模仿表单上传文件
  4. Android 之布局(一)
  5. Android开发学习笔记(五)Android五大布局
  6. 【Android基础入门No.1】Android中的几个布局
  7. Android >> 26. RecyclerView(二)— 实现横向滚动和瀑布流布局

随机推荐

  1. 2011.09.01(4)——— android 应用程序跳转
  2. android webview模拟网页post操作
  3. android scrollview嵌套listview出现高度
  4. Android(安卓)HAL:helloworld例程
  5. 解决jdk1.6已经安装,编译android源码报错
  6. Android中音频文件的使用
  7. [置顶] Android实用代码集
  8. android 音频视频合并
  9. Android(安卓)6.0 CoordinatorLayout
  10. Android(安卓)线程间通信------handler