How to set Tab width in Tab layout?

*爱你&永不变心* 提交于 2019-12-05 13:24:41

问题


I'm trying to create a tab layout which has two tabs. When I run the app on small mobile the tab layout looks fine but when I run the same application on Tablet it shows like this.

It looks like this on Tablet:

I want each tab to occupy entire space without any gaps on both the ends. I have created tab layout like this...

private void drawTabLayout() {
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("Imprint") );
    tabLayout.addTab(tabLayout.newTab().setText("Legal"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    tabLayout.setSelectedTabIndicatorHeight(10);
    tabLayout.setBackgroundColor(Color.WHITE);
}

I have searched many sites but couldn't find the right answer.


回答1:


Add this code in your tab_layout.xml

<android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabMode="fixed" />

Hope it will works for you...




回答2:


You can create a tab layout where each tab has a custom View and add that tab in the tablayout with whatever size of your choice

View customView = LayoutInflater.from(getContext()).inflate(R.layout.tab_text, null); 

addTab(newTab().setCustomView(customView))


来源:https://stackoverflow.com/questions/35336641/how-to-set-tab-width-in-tab-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!