Android 中 TabHost 的 TabWidget 显示问题

当布局文件是这样的时候:

 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.smtsokt.activity.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="11" >
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >
        </TabWidget>
    </LinearLayout>
</TabHost>

显示为:
图片说明

TabWidget的宽度改为 wrap_content 时:

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >
        </TabWidget>

显示为:
图片说明

我怎样才能让这几个选项卡平铺在下方呢?

终于找到大神相助

 private void setWeight(TabWidget tabWidget)
    {
        WindowManager windowManager = getWindowManager();
        Display windowDisplay = windowManager.getDefaultDisplay();
        int windowWidth = windowDisplay.getWidth();
        int tabWidth = windowWidth / 4; // 宽度比为:3:4:4:6:9
        tabWidget.getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(tabWidth, LayoutParams.FILL_PARENT));
        tabWidget.getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams(tabWidth, LayoutParams.FILL_PARENT));
        tabWidget.getChildAt(2).setLayoutParams(new LinearLayout.LayoutParams(tabWidth, LayoutParams.FILL_PARENT));
        tabWidget.getChildAt(3).setLayoutParams(new LinearLayout.LayoutParams(tabWidth, LayoutParams.FILL_PARENT));
    }

这样是ok的:
<?xml version="1.0" encoding="utf-8"?>
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right|center_vertical" />

</LinearLayout>

这是我很久以前联系tab的一个demo,你对比看看
主布局文件:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
TabHost的id为"@+id/tabhost"
-->
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp">
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/tab1_txt"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="the content of tab1"
                    android:textSize="20sp"/>
            </LinearLayout>
            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/tab2_txt"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="the content of tab2"
                    android:textSize="20sp"/>
            </LinearLayout>
            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/tab3_txt"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="the content of tab3"
                    android:textSize="20sp"/>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

主Activity文件:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost1);

    TabHost tabHost = (TabHost) MyTab1.this.findViewById(R.id.tabhost);
    tabHost.setup();

    TabHost.TabSpec tabSpec01 = tabHost.newTabSpec("tab1");
    tabSpec01.setIndicator("firsttab", MyTab1.this.getResources().getDrawable(R.drawable.ic_launcher));
    tabSpec01.setContent(R.id.tab1);

    TabHost.TabSpec tabSpec02 = tabHost.newTabSpec("tab2");
    tabSpec02.setIndicator("secondtab", MyTab1.this.getResources().getDrawable(R.drawable.ic_launcher));
    tabSpec02.setContent(R.id.tab2);

    TabHost.TabSpec tabSpec03 = tabHost.newTabSpec("tab3");
    tabSpec03.setIndicator("thirdtab", MyTab1.this.getResources().getDrawable(R.drawable.ic_launcher));
    tabSpec03.setContent(R.id.tab3);

    tabHost.addTab(tabSpec01);
    tabHost.addTab(tabSpec02);
    tabHost.addTab(tabSpec03);
}

我觉得你是水平放置按钮的,应该把TabWidget 的宽高改成 android:layout_width="0dp" android:layout_height="wrap_content",

不知道是不是这个问题,可以试试

Tab过时了,看看我的博客:
http://blog.csdn.net/logicteamleader/article/details/45487263