Java 如何在选项卡布局中设置选项卡宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35336641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:34:07  来源:igfitidea点击:

How to set Tab width in Tab layout?

javaandroidandroid-tablayout

提问by

I'm trying to create a tab layoutwhich has two tabs. When I run the app on small mobile the tab layoutlooks fine but when I run the same application on Tabletit shows like this.

我正在尝试创建一个tab layout有两个tabs. 当我在小型移动tab layout设备上运行该应用程序时,Tablet它看起来不错,但是当我在它上面运行相同的应用程序时,它显示为这样。

It looks like this on Tablet:

在平板电脑上看起来是这样的:

enter image description here

在此处输入图片说明

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

我希望每个标签都占据整个空间,两端没有任何间隙。我已经创建了tab layout这样的......

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.

我搜索了很多网站,但找不到正确的答案

采纳答案by HassanUsman

Add this code in your tab_layout.xml

将此代码添加到您的 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...

希望它对你有用...

回答by Rohit

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

您可以创建一个选项卡布局,其中每个选项卡都有一个自定义视图,并使用您选择的任何大小在 tablayout 中添加该选项卡

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

addTab(newTab().setCustomView(customView))