使用android中的多个选项卡调整tabhost?

时间:2014-01-03 06:01:11

标签: android android-tabhost

我有一个tabhost,共有6个标签。当我在平板电脑上打开这个应用程但是当我在较小的设备中打开它时,标签会如下所示变得拥挤。

enter image description here

我希望这些标签看起来像这样。

enter image description here

可能是滚动视图会这样做。但我无法做到。我尝试了水平滚动视图,但随后一个选项卡的宽度超出了屏幕限制。用户必须继续滚动才能转到第二个标签页。

这是我的xml代码。

<?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:orientation="vertical"
        android:layout_width="fill_parent"

        android:layout_height="fill_parent">


         <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
             android:layout_above="@android:id/tabs"
            android:layout_weight="1"
            android:layout_height="fill_parent"/>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/subbgg" />


    </LinearLayout>
</TabHost>

这是我的java代码。

public class Register extends TabActivity {
    //TabHost tabHost;
    public static TabHost tabHost;

    // Button logout;
    @Override
    public void onBackPressed() {

        //Register.this.finish();
        //super.onBackPressed();
    }

    public Register() {


    }
    public Register(int tabindex){


    }

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.register);
        if( getIntent().getBooleanExtra("Exit me", false)){
            finish();
            return; // add this to prevent from doing unnecessary stuffs
        }
        SharedPreferences app_preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        String text = app_preferences.getString("key", "null");
        tabHost = getTabHost();

        TabSpec profileTab = tabHost.newTabSpec("tid1");
        TabSpec listTab = tabHost.newTabSpec("tid2");
         TabSpec scanTab = tabHost.newTabSpec("tid3");
        TabSpec collaborationsTab = tabHost.newTabSpec("tid4");
        TabSpec logoutTab = tabHost.newTabSpec("tid5");
        TabSpec contactsTab= tabHost.newTabSpec("tid6");
        TabSpec startnewTab=tabHost.newTabSpec("tid7");

        View tabIndicator1 = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, tabHost.getTabWidget(), false);
        TextView title1 = (TextView) tabIndicator1.findViewById(R.id.title);
        ImageView icon1 = (ImageView) tabIndicator1.findViewById(R.id.icon);

        View tabIndicator2 = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, tabHost.getTabWidget(), false);
        TextView title2 = (TextView) tabIndicator2.findViewById(R.id.title);
        ImageView icon2 = (ImageView) tabIndicator2.findViewById(R.id.icon);

        final View tabIndicator3 = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, tabHost.getTabWidget(), false);
        final TextView title3 = (TextView) tabIndicator3.findViewById(R.id.title);
        ImageView icon3 = (ImageView) tabIndicator3.findViewById(R.id.icon);

        View tabIndicator4 = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, tabHost.getTabWidget(), false);
        TextView title4 = (TextView) tabIndicator4.findViewById(R.id.title);
        ImageView icon4 = (ImageView) tabIndicator4.findViewById(R.id.icon);

        View tabIndicator5 = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, tabHost.getTabWidget(), false);
        TextView title5 = (TextView) tabIndicator5.findViewById(R.id.title);
        ImageView icon5 = (ImageView) tabIndicator5.findViewById(R.id.icon);

        View tabIndicator6 = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, tabHost.getTabWidget(), false);
        TextView title6 = (TextView) tabIndicator6.findViewById(R.id.title);
        ImageView icon6 = (ImageView) tabIndicator6.findViewById(R.id.icon);

        View tabIndicator7 = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, tabHost.getTabWidget(), false);
        TextView title7 = (TextView) tabIndicator7.findViewById(R.id.title);
        ImageView icon7 = (ImageView) tabIndicator7.findViewById(R.id.icon);



        Drawable detailIcon = getResources().getDrawable(R.drawable.profile);
        //Intent detailPage = new Intent(this, Detail.class);
        Intent profilePage = new Intent(this, Profile.class);

        title1.setText("Profile");
        //title1.setTextSize(15);
        title1.setTextSize(16);
        title1.setTypeface(null, Typeface.NORMAL);
        //title1.setPadding(0, 0, 0, 20);

        icon1.setImageResource(R.drawable.profile);

        profileTab.setIndicator(tabIndicator1).setContent(profilePage);
        //detailTab.setIndicator("", detailIcon).setContent(detailPage);


        Drawable signIcon = getResources().getDrawable(R.drawable.joined);
        Intent collaPage = new Intent(this, collaborations.class);

        //signPage.putExtra("g0", stopmModel);
        title4.setText("Joined");
        title4.setTextSize(16);
        //title4.setPadding(0, 0, 0, 20);
        //title4.setTextAppearance(this, android.R.style.TextAppearance_Medium);
        title4.setTypeface(null, Typeface.NORMAL);
        icon4.setImageResource(R.drawable.joined);
        collaborationsTab.setIndicator(tabIndicator4).setContent(collaPage);
        collaPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);





        Drawable commentIcon = getResources().getDrawable(R.drawable.managed);
        Intent listPage = new Intent(this, List.class);

        //commentPage.putExtra("g0", stopmModel);
        title2.setText("Managed");
        title2.setTextSize(16);
        //title2.setTextAppearance(this, android.R.style.TextAppearance_Medium);
        title2.setTypeface(null, Typeface.NORMAL);
        //title2.setPadding(0, 0, 0, 20);
        icon2.setImageResource(R.drawable.managed);
        listTab.setIndicator(tabIndicator2).setContent(listPage);
        listPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Drawable cameraIcon = getResources().getDrawable(R.drawable.collaborations);
         final Intent scanPage = new Intent(this, Camera.class);
        //Intent cameraPage = new Intent(this, NewStop.class);

        //cameraPage.putExtra("g0", stopmModel);
        title3.setText("Scan");
    title3.setTextSize(16);
    //  title3.setTextAppearance(this, android.R.style.TextAppearance_Medium);
        title3.setTypeface(null, Typeface.NORMAL);
    //  title3.setPadding(0, 0, 0, 20);
        icon3.setImageResource(R.drawable.collaborations);
        scanTab.setIndicator(tabIndicator3).setContent(scanPage);
        //scanPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


        //Drawable contacticon = getResources().getDrawable(R.drawable.logoutone);
        Intent contactpage = new Intent(this, contact.class);

        //surveyPage.putExtra("g0", stopmModel);
        title6.setText("Contacts");
        title6.setTextSize(16);
        //title5.setPadding(0, 0, 0, 20);
        title6.setTypeface(null, Typeface.NORMAL);
        icon6.setImageResource(R.drawable.contacts);

        contactsTab.setIndicator(tabIndicator6).setContent(contactpage);






        Drawable surveyIcon = getResources().getDrawable(R.drawable.logoutone);
        Intent logoutPage = new Intent(this, Logout.class);

        //surveyPage.putExtra("g0", stopmModel);
        title5.setText("Logout");
        title5.setTextSize(16);
        //title5.setPadding(0, 0, 0, 20);
        title5.setTypeface(null, Typeface.NORMAL);
        icon5.setImageResource(R.drawable.logoutone);
        icon5.setPadding(0, 8, 0, 0);
        logoutTab.setIndicator(tabIndicator5).setContent(logoutPage);

        Intent startnewpage = new Intent(this, Startnew.class);

        //surveyPage.putExtra("g0", stopmModel);
        title7.setText("Start New");
        title7.setTextSize(16);
        //title5.setPadding(0, 0, 0, 20);
        title7.setTypeface(null, Typeface.NORMAL);
        icon7.setImageResource(R.drawable.collaborations);

        startnewTab.setIndicator(tabIndicator7).setContent(startnewpage);



        tabHost.addTab(collaborationsTab);
        tabHost.addTab(listTab);
        tabHost.addTab(scanTab);
        tabHost.addTab(profileTab);
        tabHost.addTab(startnewTab);
        tabHost.addTab(contactsTab);
        tabHost.addTab(logoutTab);

2 个答案:

答案 0 :(得分:3)

编辑:

<?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:orientation="vertical"
        android:layout_width="fill_parent"

        android:layout_height="fill_parent">


         <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
             android:layout_above="@android:id/tabs"
            android:layout_weight="1"
            android:layout_height="fill_parent">



         </FrameLayout>


         <HorizontalScrollView
             android:id="@+id/horizontalScrollView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true">



                     <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/subbgg" />


             </HorizontalScrollView>





    </LinearLayout>
</TabHost>

根据您的尺寸将其添加到您的java文件中。

tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 50;

答案 1 :(得分:1)

我通过动态获取android屏幕宽度并减少tab小部件的文本大小来实现这一点

int width = WindowHeighAndWidth.getScreenWidth(TabHostAct.this);

    if(width<542){
        titleTv.setTextSize(12.0f);
                titleSaleTV.setTextSize(12.0f);
        titlePurchTV.setTextSize(12.0f);
                titlePayTV.setTextSize(12.0f);
        titleStockTV.setTextSize(12.0f);
    }

这里是WindowHeightAndWidth类:

public class WindowHeighAndWidth {public static int getScreenWidth(Context context){
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(dm);
    int widthInPx = dm.widthPixels;
    return widthInPx;
    }
}

titlePurchTv是一个TextView,我从布局中获取并在`

上设置
spec = tabHost.newTabSpec("home"); // Create a new TabSpec using tab host
   // spec.setIndicator("Sale"); // set the “HOME” as an indicator
    spec.setIndicator(homeView);  //here you can set a layout

我希望它会对你有所帮助。如有任何问题,请发表评论