如何更改FragmentTabHost中每个选项卡的宽度

时间:2014-11-15 08:10:44

标签: android tabs fragment android-tabhost

我在android中使用此代码创建选项卡。但我不能改变每个标签的宽度。我需要帮助你。因为我是android的新手。我想要每个标签的宽度,设置为WRAP_CONTENT,但这段代码不起作用!请帮我。请给我一个明确的答案,因为我是Android的新手,我不知道更多的事情。坦克你。

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
    // Fragment TabHost as mTabHost
    private FragmentTabHost mTabHost;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("tab5").setIndicator(getString(R.string.BestSellerOfWeekFreeTab_String)),
                BestSellerOfWeekFreeTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab6").setIndicator(getString(R.string.BestSellerOfWeekSellTab_String)),
                BestSellerOfWeekSellTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab7").setIndicator(getString(R.string.HotDayTab_String)),
                HotDayTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab8").setIndicator(getString(R.string.OfferTab_String)),
                OfferTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab9").setIndicator(getString(R.string.OngoingTab_String)),
                OngoingTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab10").setIndicator(getString(R.string.PopularTab_String)),
                PopularTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator(getString(R.string.BestNewestSellTab_String)),
                BestNewestSellTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator(getString(R.string.BestNewestFreeTab_String)),
                BestNewestFreeTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(getString(R.string.NewestTab_String)),
                NewestTab.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(getString(R.string.HomeTab_String)),
                HomeTab.class, null);
        mTabHost.setCurrentTab(9);

        for(int i =0; i<10;i++){
            TextView x = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            x.setTextSize(15);
            mTabHost.getTabWidget().getChildAt(i).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
        }



    }

}

我的xml是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <android.support.v4.app.FragmentTabHost

        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">
            <TabWidget
                android:id="@android:id/tabs"

                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"/>
            </HorizontalScrollView>

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


            <FrameLayout
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

请帮助meeeeee: - (

0 个答案:

没有答案
相关问题