Android: 可以横向拖动的TabHost

横向拖动的TabHost 效果图:

Xml代码

  1. <LinearLayout android:orientation="vertical"
  2. android:layout_;fill_parent" android:layout_height="fill_parent">
  3.  
  4. <RelativeLayout android:layout_;fill_parent"
  5. android:layout_height="wrap_content">
  6.  
  7. <HorizontalScrollView android:layout_;fill_parent"
  8. android:layout_height="wrap_content"
  9. android:fillViewport="true"
  10. android:scrollbars="none"
  11. android:layout_toLeftOf="@+id/next_button"
  12. android:layout_toRightOf="@+id/up_button">
  13.  
  14. <TabWidget android:id="@android:id/tabs"
  15. android:layout_;fill_parent"
  16. android:layout_height="wrap_content" />
  17.  
  18. </HorizontalScrollView>
  19. </RelativeLayout>
  20.  
  21. <FrameLayout android:id="@android:id/tabcontent"
  22. android:layout_;fill_parent" android:layout_height="fill_parent">
  23.  
  24. <TextView android:id="@+id/textview01" android:layout_;fill_parent"
  25. android:layout_height="wrap_content" android:text="@string/hello" />
  26.  
  27. <TextView android:id="@+id/textview02" android:layout_;fill_parent"
  28. android:layout_height="wrap_content" android:text="@string/hello" />
  29.  
  30. <TextView android:id="@+id/textview03" android:layout_;fill_parent"
  31. android:layout_height="wrap_content" android:text="@string/hello" />
  32.  
  33. <TextView android:id="@+id/textview04" android:layout_;fill_parent"
  34. android:layout_height="wrap_content" android:text="@string/hello" />
  35.  
  36. <TextView android:id="@+id/textview05" android:layout_;fill_parent"
  37. android:layout_height="wrap_content" android:text="@string/hello" />
  38. </FrameLayout>
  39.  
  40. </LinearLayout>

<LinearLayout android:orientation="vertical" android:layout_;fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true" android:scrollbars="none" android:layout_toLeftOf="@+id/next_button" android:layout_toRightOf="@+id/up_button"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </HorizontalScrollView> </RelativeLayout> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textview01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/textview02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/textview03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/textview04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/textview05" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </FrameLayout> </LinearLayout>

Java代码

  1. import android.app.TabActivity;  
  2. import android.os.Bundle;  
  3. import android.util.DisplayMetrics;  
  4. import android.util.Log;  
  5. import android.widget.Button;  
  6. import android.widget.TabHost;  
  7. import android.widget.TabWidget;  
  8.  
  9. public class MainActivity extends TabActivity {  
  10.     TabHost m_TabHost;  
  11.     Button upButton;  
  12.     Button nextButton;  
  13.  
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {  
  16. super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18. // Init
  19.         initView();  
  20.         initTabHost();  
  21.  
  22. // Get scream width
  23.         DisplayMetrics dm = new DisplayMetrics();  
  24.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  25. int screenWidth = dm.widthPixels;  
  26.         Log.i("test", "screen; + screenWidth);  
  27.  
  28. // Get tab counts
  29.         TabWidget tabWidget = m_TabHost.getTabWidget();  
  30. int count = tabWidget.getChildCount();  
  31. if (count &gt; 3) {  
  32. for (int i = 0; i &lt; count; i++) {  
  33.                 tabWidget.getChildTabViewAt(i).setMinimumWidth((screenWidth) / 3);  
  34.             }  
  35.         }  
  36.     }  
  37.  
  38. private void initView() {  
  39.         m_TabHost = getTabHost();  
  40. // upButton = (Button) findViewById(R.id.up_button);
  41. // nextButton = (Button) findViewById(R.id.next_button);
  42.     }  
  43.  
  44. private void initTabHost() {  
  45.         m_TabHost.addTab(m_TabHost.newTabSpec(0 + "").setIndicator("A  eoe")  
  46.                 .setContent(R.id.textview01));  
  47.         m_TabHost.addTab(m_TabHost.newTabSpec(1 + "").setIndicator("B  eoe")  
  48.                 .setContent(R.id.textview01));  
  49.         m_TabHost.addTab(m_TabHost.newTabSpec(2 + "").setIndicator("C  eoe")  
  50.                 .setContent(R.id.textview01));  
  51.         m_TabHost.addTab(m_TabHost.newTabSpec(3 + "").setIndicator("D  eoe")  
  52.                 .setContent(R.id.textview01));  
  53.         m_TabHost.addTab(m_TabHost.newTabSpec(4 + "").setIndicator("E  eoe")  
  54.                 .setContent(R.id.textview01));  
  55.         m_TabHost.addTab(m_TabHost.newTabSpec(5 + "").setIndicator("F  eoe")  
  56.                 .setContent(R.id.textview01));  
  57.         m_TabHost.addTab(m_TabHost.newTabSpec(6 + "").setIndicator("G  eoe")  
  58.                 .setContent(R.id.textview01));  
  59.     }  

你可能感兴趣的:(android,移动开发,tabhost,休闲,横向拖动)