tablayout 滚动模式_在Tablayout中使滚动或固定模式获得动态的Tabs数量

I want tabMode scrollable with tabFilled with tabs(having single line title). As number of Tab and their title is dynamic in my app. How could I handle them

when I set given property. Title of tabs come's in two lines.when number of tab is greater.

app:tabGravity="fill"

app:tabMaxWidth="0dp"

app:tabMode="fixed"

and when number of tabs are less and tabMode is set to scrollable. then extra space shown like below.

how could I make a Tablayout to work in all phones and with one line text .

Simply remove spaces for second screen and make single line for 1st Image.

I know I have to change it's tabMode and tabGravity for these two cases but How could I know when to change it's tabMode and tabGravity. It may be a case that I have only three tab's but their title are long and they are filling entire space. and some time we have 5 tabs with little title . They easily fit's in screen.

解决方案I come up with a new and better solution..

List titleTabs = getTitleOfTab();

for (String module : titleTabs) {

mTabLayout.addTab(mTabLayout.newTab().setText(module));

}

mTabLayout.post(new Runnable()

{

@Override

public void run()

{

// don't forget to add Tab first before measuring..

DisplayMetrics displayMetrics = new DisplayMetrics();

((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

int widthS = displayMetrics.widthPixels;

mTabLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

int widthT = mTabLayout.getMeasuredWidth();

if (widthS > widthT) {

mTabLayout.setTabMode(TabLayout.MODE_FIXED);

mTabLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT));

}

}

});

where tablayout in xml must have tabGravity Fill and tabMode to Scrollable

android:id="@+id/tab_layout"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:minHeight="?attr/actionBarSize"

app:tabGravity="fill"

app:tabIndicatorHeight="4dp"

app:tabMode="scrollable"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值