xml方式的 android的 tabhost用法实例

前端之家收集整理的这篇文章主要介绍了xml方式的 android的 tabhost用法实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package com.oschina.osmf.study;
  2.  
  3.  
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.widget.TabHost;
  7. import android.widget.TabHost.OnTabChangeListener;
  8. import android.widget.Toast;
  9.  
  10.  
  11. import com.oschina.osmf.BaseActivity;
  12. import com.oschina.osmf.R;
  13.  
  14.  
  15. public class StudyTabHostActivity extends BaseActivity {
  16.  
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. // TODO Auto-generated method stub
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.studytabhost);
  23. TabHost host = (TabHost) findViewById(R.id.tabhost);
  24. host.setup();
  25.  
  26.  
  27. TabHost.TabSpec homeSpec = host.newTabSpec("Home"); // This param will
  28. // be used as tabId.
  29. homeSpec.setIndicator(null,// This param will diplay as title.
  30. getResources().getDrawable(R.drawable.buju));
  31. homeSpec.setContent(R.id.tab1);
  32. host.addTab(homeSpec);
  33.  
  34.  
  35. TabHost.TabSpec garbageSpec = host.newTabSpec("Garbage");
  36. garbageSpec.setIndicator(null,getResources().getDrawable
  37.  
  38.  
  39. (R.drawable.buju));
  40. garbageSpec.setContent(R.id.tab2);
  41. host.addTab(garbageSpec);
  42.  
  43.  
  44. TabHost.TabSpec maybeSpec = host.newTabSpec("Help");
  45. maybeSpec.setIndicator(null,getResources().getDrawable
  46.  
  47.  
  48. (R.drawable.buju));
  49. maybeSpec.setContent(R.id.tab3);
  50. host.addTab(maybeSpec);
  51.  
  52.  
  53. host.setOnTabChangedListener(new OnTabChangeListener() {
  54.  
  55.  
  56. @Override
  57. public void onTabChanged(String tabId) {
  58. // TODO Auto-generated method stub
  59. Toast toast = Toast.makeText(StudyTabHostActivity.this,tabId,Toast.LENGTH_SHORT);
  60. toast.setGravity(Gravity.CENTER_HORIZONTAL,50);
  61. toast.show();
  62. }
  63. });
  64.  
  65.  
  66. // host.setCurrentTabByTag("Home");
  67. Toast toast = Toast.makeText(StudyTabHostActivity.this,"Home",Toast.LENGTH_SHORT);
  68. toast.setGravity(Gravity.CENTER_HORIZONTAL,50);
  69. toast.show();
  70. }
  71. }


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/tabhost"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6.  
  7. <RelativeLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent" >
  10.  
  11. <TabWidget
  12. android:id="@android:id/tabs"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentBottom="true" >
  16. </TabWidget>
  17. <!-- set the tab body attributes -->
  18.  
  19. <FrameLayout
  20. android:id="@android:id/tabcontent"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent" >
  23.  
  24. <LinearLayout
  25. android:id="@+id/tab1"
  26. android:layout_width="fill_parent"
  27. android:layout_height="fill_parent"
  28. android:orientation="vertical"
  29. android:paddingTop="20dip" >
  30.  
  31. <ImageView
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_gravity="center" />
  35. </LinearLayout>
  36.  
  37. <LinearLayout
  38. android:id="@+id/tab2"
  39. android:layout_width="fill_parent"
  40. android:layout_height="fill_parent"
  41. android:orientation="vertical"
  42. android:paddingTop="20dip" >
  43.  
  44. <ImageView
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:layout_gravity="center" />
  48. </LinearLayout>
  49.  
  50. <LinearLayout
  51. android:id="@+id/tab3"
  52. android:layout_width="fill_parent"
  53. android:layout_height="fill_parent"
  54. android:orientation="vertical"
  55. android:paddingTop="20dip" >
  56.  
  57. <ImageView
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_gravity="center" />
  61. </LinearLayout>
  62. </FrameLayout>
  63. </RelativeLayout>
  64.  
  65. </TabHost>

猜你在找的XML相关文章