搜尋此網誌

2015年8月26日 星期三

【Android】Android Support Design TabLayout: Gravity Center and Mode Scrollable

簡單來說,問題是

設定tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

手機直立顯示,Tab在畫面上看起來很正常,超出螢幕的寬度,可以用scroolable解。



當手機橫置顯示時,Tab的總寬度小於螢幕寬度太多,造成視覺上很奇怪,如何解?

答案:

As I didn't find why does this behaviour happen I have used the following code:
float myTabLayoutSize = 360;
if (DeviceInfo.getWidthDP(this) >= myTabLayoutSize ){
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
} else {
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}
Basically, I have to calculate manually the width of my tabLayout and then I set the Tab Mode depending on if the tabLayout fits in the device or not.
The reason why I get the size of the layout manually is because not all the tabs have the same width in Scrollable mode, and this could provoke that some names use 2 lines as it happened to me in the example.

在不同顯示下,使用不同的顯示方式。
上述解答,應需加
app:tabGravity="fill"
才可以達到想要的效果。

沒有留言: