tablayout 滚动模式_支持Android设计TabLayout:重心和模式滚动

I am trying to use the new Design TabLayout in muy project. I want the layout to adapt to every screen size and orientation, but it can be seen correclty in one orientantion.

I am dealing with Gravity and Mode setting my tabLayout as:

tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);

tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

So I expect that if there is no room, the tabLayout is scrollable, but if there is room, it is centered.

From the guides:

public static final int GRAVITY_CENTER Gravity used to lay out the

tabs in the center of the TabLayout.

public static final int GRAVITY_FILL Gravity used to fill the

TabLayout as much as possible. This option only takes effect when used

with MODE_FIXED.

public static final int MODE_FIXED Fixed tabs display all tabs

concurrently and are best used with content that benefits from quick

pivots between tabs. The maximum number of tabs is limited by the

view’s width. Fixed tabs have equal width, based on the widest tab

label.

public static final int MODE_SCROLLABLE Scrollable tabs display a

subset of tabs at any given moment, and can contain longer tab labels

and a larger number of tabs. They are best used for browsing contexts

in touch interfaces when users don’t need to directly compare the tab

labels.

So GRAVITY_FILL is compatible only with MODE_FIXED but, at is doesn't specify anything for GRAVITY_CENTER, I expect it to be compatible with MODE_SCROLLABLE, but this is what I get using GRAVITY_CENTER and MODE_SCROLLABLE

So it is using SCROLLABLE in both orientations, but it is not using GRAVITY_CENTER.

This is what I would expect for landscape; but to have this, I need to set MODE_FIXED, so what I get in portrait is:

Why is GRAVITY_CENTER not working for SCROLLABLE if the tabLayout fits the screen?

Is there any way to set dinamically gravity and mode (and to see what I am expecting)?

Thank you very much!

EDITED: This is the Layout of my TabLayout:

android:id="@+id/sliding_tabs"

android:layout_width="match_parent"

android:background="@color/orange_pager"

android:layout_height="wrap_content" />

解决方案

Tab gravity only effects MODE_FIXED.

One possible solution is to set your layout_width to wrap_content and layout_gravity to center_horizontal:

android:id="@+id/sliding_tabs"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

app:tabMode="scrollable" />

If the tabs are smaller than the screen width, the TabLayout itself will also be smaller and it will be centered because of the gravity. If the tabs are bigger than the screen width, the TabLayout will match the screen width and scrolling will activate.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值