改变android tablayout引力

时间:2016-07-22 13:39:21

标签: android android-layout

我将实现滚动功能到我的TabLayout。它成功滚动但是标签重力根本不起作用。我的标签与视图的右侧对齐。我想在中心位置显示这些标签或者至少填充layout_width。

我在下面添加了我的代码。请建议让事情有效的可行方法。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        app:tabMode="scrollable"
        app:tabGravity="fill"/>

</android.support.design.widget.AppBarLayout>

这是tablayout的屏幕截图。

enter image description here

1 个答案:

答案 0 :(得分:0)

魔法存在于引力模式,在你的情况下你需要GRAVITY_CENTER。将您的代码更改为:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        app:tabMode="scrollable"
        app:tabGravity="center"/>

</android.support.design.widget.AppBarLayout>