標籤:
Android中選項卡功能的實現
Android中使用TabHost和TabWidget來實現選項卡功能。TabHost必須是布局的根節點,它包含兩個子節點:
TabWidget,顯示選項卡;
FrameLayout,顯示標籤內容。
實現選項卡功能有兩種方法,一種是將多個View放在同一個Activity中,然後使用使用標籤來進行切換。另一種是直接使用標籤切換不同的Activity。
後一種方法更為常用一些。本文也只介紹了後一種方法的實現過程。
1. 建立一個工程,名字可以叫HelloTabWidget。
2. 建立多個不同的Activity,用來表示各個標籤頁中的不同內容。
3. 為標籤設計不同的icon。每個標籤應該有兩個icon,一個表示選中,一個未選中。將圖片放在 res/drawable/檔案夾下。然後建立一個相應的
StateListDrawable,用來實現在選中和未選中直接自動的切換。
[java] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- When selected, use grey -->
- <item android:drawable="@drawable/ic_tab_artists_grey"
- android:state_selected="true" />
- <!-- When not selected, use white-->
- <item android:drawable="@drawable/ic_tab_artists_white" />
- </selector>
4. 將main.xml替換為以下內容。
[java] view plaincopy
- <?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"
- android:padding="5dp">
- <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"
- android:padding="5dp" />
- </LinearLayout>
- </TabHost>
5. 讓你的主Activity繼承自TabActivity。
6. 在主Activity的onCreate方法中加入標籤
[java] view plaincopy
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- Resources res = getResources(); // Resource object to get Drawables
- TabHost tabHost = getTabHost(); // The activity TabHost
- TabHost.TabSpec spec; // Resusable TabSpec for each tab
- Intent intent; // Reusable Intent for each tab
-
- // Create an Intent to launch an Activity for the tab (to be reused)
- intent = new Intent().setClass(this, ArtistsActivity.class);
-
- // Initialize a TabSpec for each tab and add it to the TabHost
- spec = tabHost.newTabSpec("artists").setIndicator("Artists",
- res.getDrawable(R.drawable.ic_tab_artists))
- .setContent(intent);
- tabHost.addTab(spec);
-
- // Do the same for the other tabs
- intent = new Intent().setClass(this, AlbumsActivity.class);
- spec = tabHost.newTabSpec("albums").setIndicator("Albums",
- res.getDrawable(R.drawable.ic_tab_albums))
- .setContent(intent);
- tabHost.addTab(spec);
-
- intent = new Intent().setClass(this, SongsActivity.class);
- spec = tabHost.newTabSpec("songs").setIndicator("Songs",
- res.getDrawable(R.drawable.ic_tab_songs))
- .setContent(intent);
- tabHost.addTab(spec);
-
- tabHost.setCurrentTab(2);
- }
7. 運行程式即可看到效果。
總結:
main.xml中定義的選項卡的樣式,它和TabActivity共同配合,建立了選項卡的架構。TabHost.TabSpec 代表了一個選項卡的內容,TabHost使用addTab方法將其加入到整個選項卡中。
TabHost.TabSpec
原文: http://blog.csdn.net/cool_android/article/details/7202381
Android中選項卡功能的實現