Android中TabHost切換不同的Activity

來源:互聯網
上載者:User

TabHost在很多應用都會使用到,有時候在TabHost添加的Tab中設定view不能滿足需求,因為在view中添加如PreferenceActivity相當困難.

之前在一個應用中需要實現使用TabHost中在多個Tab切換不同的Activity.一個是日誌列表(ListActivity),一個是應用設定頁面( PreferenceActivity )

先上

是日誌列表頁面,是ListActivity

是設定頁面,是一般的PreferenceActivity

開發流程 :
1, 編寫不同的Activity,用於在TabHost中點擊不同的Tab時進行切換(以下是兩個例子,因為不是該博文的主要內容所以省略)
1-1, 編寫LogActivity,該類是ListActivity,用於顯示日誌內容
1-2, 編寫SettingActivity,該類是PreferenceActivity,應用的設定頁面,編寫方式與PreferenceActivity一樣.

2, 編寫首頁面(MainActivity),該類是應用的入口類,在該類定義TabHost並添加相應的Activity.

// 該類需要繼承ActivityGrouppublic class MainActivity extends ActivityGroup {  private TabHost mTabHost;   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    this.requestWindowFeature(Window.FEATURE_NO_TITLE);    setContentView(R.layout.main);    // 設定TabHost    initTabs();  }   private void initTabs() {    mTabHost = (TabHost) findViewById(R.id.tabhost);    mTabHost.setup(this.getLocalActivityManager());    // 添加日誌列表的tab,注意下面的setContent中的代碼.是這個需求實現的關鍵    mTabHost.addTab(mTabHost.newTabSpec("tab_log")      .setIndicator("日誌",getResources().getDrawable(R.drawable.ic_tab_log))      .setContent(new Intent(this, LogActivity.class)));     // 添加應用設定的tab,注意下面的setContent中的代碼.是這個需求實現的關鍵    mTabHost.addTab(mTabHost.newTabSpec("tab_setting")      .setIndicator("設定",getResources().getDrawable(R.drawable.ic_tab_settings))      .setContent(new Intent(this, SettingActivity.class)));    mTabHost.setCurrentTab(1);  }}

3, 編寫main.xml,MainActivity的layout的xml設定檔

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"xmlns:app="http://schemas.android.com/apk/res/rbase.app.nowscore"android:layout_height="fill_parent"                 android:layout_above="@+id/adLayout"><TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tabhost" android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent"                                 android:layout_height="wrap_content"><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" /></LinearLayout></TabHost></LinearLayout></RelativeLayout>

  補充:
當在不同的Tab中按下menu按鈕,會以當前的Activity為準,即在SettingActivity時按下menu按鈕後會觸發SettingActivity的事件,不是首頁面的事件.

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.