Android选项卡的几种实现方法

最近在解决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 >

代码如下:

[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));
     }
}

你可能感兴趣的:(Android选项卡的几种实现方法)