import android.app.TabActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TabHost;
import android.widget.TabWidget;
public class MainActivity extends TabActivity {
TabHost tabhost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabhost = getTabHost();
// 7个标签
tabhost.addTab(tabhost.newTabSpec(0 + "").setIndicator("测试")
.setContent(R.id.textview01));
tabhost.addTab(tabhost.newTabSpec(1 + "").setIndicator("标题")
.setContent(R.id.textview01));
tabhost.addTab(tabhost.newTabSpec(2 + "").setIndicator("办公")
.setContent(R.id.textview01));
tabhost.addTab(tabhost.newTabSpec(3 + "").setIndicator("通讯")
.setContent(R.id.textview01));
tabhost.addTab(tabhost.newTabSpec(4 + "").setIndicator("邮件")
.setContent(R.id.textview01));
tabhost.addTab(tabhost.newTabSpec(5 + "").setIndicator("私信")
.setContent(R.id.textview01));
tabhost.addTab(tabhost.newTabSpec(6 + "").setIndicator("通知")
.setContent(R.id.textview01));
TabWidget tabWidget = tabhost.getTabWidget();
// 标签的个数
int count = tabWidget.getChildCount();
// 获取手机屏幕的宽高
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenheight = displayMetrics.heightPixels;
// 我的手机是:320*480
System.out.println(screenWidth + "*" + screenheight);
if (count >= 4) {
for (int i = 0; i < count; i++) {
// 设置每个标签的宽度,为屏幕的1/4
tabWidget.getChildTabViewAt(i).setMinimumWidth(
(screenWidth) / 4);
}
}
}
}
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- 水平滚动 -->
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/next_button"
android:layout_toRightOf="@+id/up_button"
android:fillViewport="true"
android:scrollbars="none" >
<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" />
<TextView
android:id="@+id/textview02"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview03"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview04"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview05"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
</TabHost>