如何在 Tab 布局中设置 Tab 宽度?

How to set Tab width in Tab layout?

我正在尝试创建一个有两个 tabstab layout。当我 运行 小型手机上的应用程序时, tab layout 看起来不错,但是当我 运行 在 Tablet 上使用相同的应用程序时,它显示如下。

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

我希望每个选项卡占据整个 space,两端没有任何间隙。我这样创建了 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);
}

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

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

希望对你有用...

您可以创建一个选项卡布局,其中每个选项卡都有一个自定义视图,然后将该选项卡添加到您选择的任意大小的选项卡布局中

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

addTab(newTab().setCustomView(customView))