增加Tab布局中的选项卡宽度

时间:2017-03-22 06:41:28

标签: android android-viewpager adapter android-tablayout

enter image description here

大家好,

我正在开发Android Material Design。

当我将模式标签添加到我的适配器时,文本会变形,分成两个位置,如图所示。 我的代码:

 private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new ListContentFragment(), "Home");
        adapter.addFragment(new TileContentFragment(), "Screen 1");
        adapter.addFragment(new CardContentFragment(), "Screen 2");
        adapter.addFragment(new ListContentFragment(), "Screen 3");
        adapter.addFragment(new TileContentFragment(), "Screen 4");
        adapter.addFragment(new CardContentFragment(), "Screen 5");


        viewPager.setAdapter(adapter);
    }

我的XML

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </android.support.design.widget.AppBarLayout>
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="@dimen/md_keylines"
            android:layout_marginRight="@dimen/md_keylines"
            app:srcCompat="@drawable/ic_add"
            android:tint="@color/white"/>
    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/navheader"
        app:menu="@menu/menu_navigation" />
</android.support.v4.widget.DrawerLayout>

如何增加标签宽度以确保6,Home,Screen1,Screen 2等在一行i,e更宽,而不是两行?

3 个答案:

答案 0 :(得分:6)

设置应用:tabMode =&#34;可滚动&#34;对于布局文件中的android.support.design.widget.TabLayout

答案 1 :(得分:2)

  <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>

答案 2 :(得分:2)

以编程方式设置

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

XML

            app:tabGravity="fill"
            app:tabMode="scrollable"

请注意,如果它不符合您的要求