Android中選項卡功能的實現

來源:互聯網
上載者:User

標籤:

 Android中選項卡功能的實現

Android中使用TabHost和TabWidget來實現選項卡功能。TabHost必須是布局的根節點,它包含兩個子節點:

TabWidget,顯示選項卡;

FrameLayout,顯示標籤內容。

 

實現選項卡功能有兩種方法,一種是將多個View放在同一個Activity中,然後使用使用標籤來進行切換。另一種是直接使用標籤切換不同的Activity。

後一種方法更為常用一些。本文也只介紹了後一種方法的實現過程。

1. 建立一個工程,名字可以叫HelloTabWidget。

2. 建立多個不同的Activity,用來表示各個標籤頁中的不同內容。

3. 為標籤設計不同的icon。每個標籤應該有兩個icon,一個表示選中,一個未選中。將圖片放在 res/drawable/檔案夾下。然後建立一個相應的

StateListDrawable,用來實現在選中和未選中直接自動的切換。

 

[java] view plaincopy 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <!-- When selected, use grey -->  
  4.     <item android:drawable="@drawable/ic_tab_artists_grey"  
  5.           android:state_selected="true" />  
  6.     <!-- When not selected, use white-->  
  7.     <item android:drawable="@drawable/ic_tab_artists_white" />  
  8. </selector>  

4. 將main.xml替換為以下內容。

 

[java] view plaincopy 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <LinearLayout  
  7.         android:orientation="vertical"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:padding="5dp">  
  11.         <TabWidget  
  12.             android:id="@android:id/tabs"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="wrap_content" />  
  15.         <FrameLayout  
  16.             android:id="@android:id/tabcontent"  
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="fill_parent"  
  19.             android:padding="5dp" />  
  20.     </LinearLayout>  
  21. </TabHost>  

 

5. 讓你的主Activity繼承自TabActivity。

6. 在主Activity的onCreate方法中加入標籤

 

[java] view plaincopy 
  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4.   
  5.     Resources res = getResources(); // Resource object to get Drawables  
  6.     TabHost tabHost = getTabHost();  // The activity TabHost  
  7.     TabHost.TabSpec spec;  // Resusable TabSpec for each tab  
  8.     Intent intent;  // Reusable Intent for each tab  
  9.   
  10.     // Create an Intent to launch an Activity for the tab (to be reused)  
  11.     intent = new Intent().setClass(this, ArtistsActivity.class);  
  12.   
  13.     // Initialize a TabSpec for each tab and add it to the TabHost  
  14.     spec = tabHost.newTabSpec("artists").setIndicator("Artists",  
  15.                       res.getDrawable(R.drawable.ic_tab_artists))  
  16.                   .setContent(intent);  
  17.     tabHost.addTab(spec);  
  18.   
  19.     // Do the same for the other tabs  
  20.     intent = new Intent().setClass(this, AlbumsActivity.class);  
  21.     spec = tabHost.newTabSpec("albums").setIndicator("Albums",  
  22.                       res.getDrawable(R.drawable.ic_tab_albums))  
  23.                   .setContent(intent);  
  24.     tabHost.addTab(spec);  
  25.   
  26.     intent = new Intent().setClass(this, SongsActivity.class);  
  27.     spec = tabHost.newTabSpec("songs").setIndicator("Songs",  
  28.                       res.getDrawable(R.drawable.ic_tab_songs))  
  29.                   .setContent(intent);  
  30.     tabHost.addTab(spec);  
  31.   
  32.     tabHost.setCurrentTab(2);  
  33. }  

7. 運行程式即可看到效果。

 

 

總結:

main.xml中定義的選項卡的樣式,它和TabActivity共同配合,建立了選項卡的架構。TabHost.TabSpec 代表了一個選項卡的內容,TabHost使用addTab方法將其加入到整個選項卡中。

TabHost.TabSpec

 

原文: http://blog.csdn.net/cool_android/article/details/7202381

Android中選項卡功能的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.