Android选项卡的几种实现方法

时间:2022-04-27 06:24:50

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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 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]

123456789101112131415161718192021222324252627282930313233343536373839 <?xml version="1.0" encoding="utf-8"?> <TabHostxmlns: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"?><TabHostxmlns: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]

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