【问题标题】:changing android tablayout gravity改变 android tablayout 重力
【发布时间】:2016-11-26 10:42:38
【问题描述】:

我要为我的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>

这是标签布局的屏幕截图。

【问题讨论】:

标签: android android-layout


【解决方案1】:

神奇之处在于重力模式,在您的情况下,您需要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>

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2018-03-11
    相关资源
    最近更新 更多