支持Android设计TabLayout:重心和模式滚动 [英] Android Support Design TabLayout: Gravity Center and Mode Scrollable
问题描述
我想使用新的设计TabLayout在MUY项目。我想要的布局,以适应每个屏幕大小和方向,但它可以correclty在一个orientantion可见。
我处理的重力和模式的设定我tabLayout为:
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
因此,我预计,如果没有房,tabLayout是滚动的,但如果有空间,它的中心。
从指南:
公共静态最终诠释GRAVITY_CENTER重力用来铺陈 标签在TabLayout的中心
公共静态最终诠释GRAVITY_FILL重力用来填补 TabLayout尽可能。此选项仅生效使用时 与MODE_FIXED。
公共静态最终诠释MODE_FIXED固定选项卡显示所有标签 同时并用最好的受益于快速的内容中使用 标签之间转动。片的最大数目由所述限制 视图的宽度。固定标签具有相等的宽度,根据最宽的标签上 标签。
公共静态最终诠释MODE_SCROLLABLE滚动标签显示 标签在任何特定时刻的子集,并且可以包含更长的标签标签 和标签的数量较多。他们是最适合用于浏览上下文 在触摸界面时用户不需要直接比较的标签 标签。
所以GRAVITY_FILL仅与MODE_FIXED兼容,但在IS不指定任何GRAVITY_CENTER,我希望它是与MODE_SCROLLABLE兼容的,但是这是我开始使用GRAVITY_CENTER和MODE_SCROLLABLE
因此,它是使用SCROLLABLE在两个方向,但它不使用GRAVITY_CENTER
这是我所期望的景观;但有这个,我需要设置MODE_FIXED,所以我得到的画像是:
为什么GRAVITY_CENTER不工作的SCROLLABLE如果tabLayout适合屏幕? 有什么办法来设置dinamically重力和模式(和看什么我期待)?
非常感谢你!
编辑:这是我的TabLayout的布局:
< android.support.design.widget.TabLayout
机器人:ID =@ + ID / sliding_tabs
机器人:layout_width =match_parent
机器人:背景=@色/ orange_pager
机器人:layout_height =WRAP_CONTENT/>
标签重力只影响 MODE_FIXED
。
一个可行的办法是设置你的 layout_width
到 WRAP_CONTENT
和 layout_gravity
到 center_horizontal
:
< android.support.design.widget.TabLayout
机器人:ID =@ + ID / sliding_tabs
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =center_horizontal
应用程序:tabMode =滚动/>
如果凸起为比屏幕的宽度越小, TabLayout
本身也将较小,因为重力将被居中。如果选项卡是大于屏幕宽度, 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.support.design.widget.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.support.design.widget.TabLayout
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.
这篇关于支持Android设计TabLayout:重心和模式滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!