横向拖动的TabHost 效果图:
Xml代码
- <LinearLayout android:orientation="vertical"
- android:layout_;fill_parent" android:layout_height="fill_parent">
- <RelativeLayout android:layout_;fill_parent"
- android:layout_height="wrap_content">
- <HorizontalScrollView android:layout_;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_;fill_parent"
- android:layout_height="wrap_content" />
- </HorizontalScrollView>
- </RelativeLayout>
- <FrameLayout android:id="@android:id/tabcontent"
- android:layout_;fill_parent" android:layout_height="fill_parent">
- <TextView android:id="@+id/textview01" android:layout_;fill_parent"
- android:layout_height="wrap_content" android:text="@string/hello" />
- <TextView android:id="@+id/textview02" android:layout_;fill_parent"
- android:layout_height="wrap_content" android:text="@string/hello" />
- <TextView android:id="@+id/textview03" android:layout_;fill_parent"
- android:layout_height="wrap_content" android:text="@string/hello" />
- <TextView android:id="@+id/textview04" android:layout_;fill_parent"
- android:layout_height="wrap_content" android:text="@string/hello" />
- <TextView android:id="@+id/textview05" android:layout_;fill_parent"
- android:layout_height="wrap_content" android:text="@string/hello" />
- </FrameLayout>
- </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代码
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.widget.Button;
- import android.widget.TabHost;
- import android.widget.TabWidget;
- public class MainActivity extends TabActivity {
- TabHost m_TabHost;
- Button upButton;
- Button nextButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // Init
- initView();
- initTabHost();
- // Get scream width
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int screenWidth = dm.widthPixels;
- Log.i("test", "screen; + screenWidth);
- // Get tab counts
- TabWidget tabWidget = m_TabHost.getTabWidget();
- int count = tabWidget.getChildCount();
- if (count > 3) {
- for (int i = 0; i < count; i++) {
- tabWidget.getChildTabViewAt(i).setMinimumWidth((screenWidth) / 3);
- }
- }
- }
- private void initView() {
- m_TabHost = getTabHost();
- // upButton = (Button) findViewById(R.id.up_button);
- // nextButton = (Button) findViewById(R.id.next_button);
- }
- private void initTabHost() {
- m_TabHost.addTab(m_TabHost.newTabSpec(0 + "").setIndicator("A eoe")
- .setContent(R.id.textview01));
- m_TabHost.addTab(m_TabHost.newTabSpec(1 + "").setIndicator("B eoe")
- .setContent(R.id.textview01));
- m_TabHost.addTab(m_TabHost.newTabSpec(2 + "").setIndicator("C eoe")
- .setContent(R.id.textview01));
- m_TabHost.addTab(m_TabHost.newTabSpec(3 + "").setIndicator("D eoe")
- .setContent(R.id.textview01));
- m_TabHost.addTab(m_TabHost.newTabSpec(4 + "").setIndicator("E eoe")
- .setContent(R.id.textview01));
- m_TabHost.addTab(m_TabHost.newTabSpec(5 + "").setIndicator("F eoe")
- .setContent(R.id.textview01));
- m_TabHost.addTab(m_TabHost.newTabSpec(6 + "").setIndicator("G eoe")
- .setContent(R.id.textview01));
- }
- }
转载于:https://blog.51cto.com/gswxr/716253