Java TabLayout 中的选项卡没有填满整个 ActionBar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30721498/
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 10:03:26  来源:igfitidea点击:

Tabs in TabLayout not filling up entire ActionBar

javaandroidtabs

提问by clever_trevor

I am using a TabLayoutand ViewPagerto display ActionBar tabsfollowing the guide Google Play Style Tabs using TabLayout, however my tabs are squished to the left side of the ActionBar, shown below:

我正在使用TabLayout使用 TabLayout按照Google Play 样式选项卡指南ViewPager进行显示,但是我的选项卡被压缩到 ActionBar 的左侧,如下所示:ActionBar tabs


And I would like them to take up the whole bar with equal widths. I've made only a few minor changes to the guide:


我希望他们以相等的宽度占据整个酒吧。我只对指南做了一些小改动:

In activity_main.xml a style was created to show the ActionBar:

在 activity_main.xml 中创建了一个样式来显示 ActionBar:

<android.support.design.widget.TabLayout
  android:id="@+id/sliding_tabs"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  style="@style/AppTheme"
  app:tabMode="scrollable" />

Here is the styles.xml code:

这是styles.xml 代码:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="windowActionBar">true</item>
  <item name="tabIndicatorColor">#ffff0030</item>
</style>

Also, my MainActivitynow extends AppCompatActivityinstead of a FragmentActivity.

另外,我MainActivity现在扩展AppCompatActivity而不是FragmentActivity.

采纳答案by Ellie Zou

You can refer to the TabLayout.

你可以参考TabLayout

GRAVITY_CENTERGravity used to lay out the tabs in the center of the TabLayout.

GRAVITY_CENTER重力用于在 TabLayout 的中心布置选项卡。

GRAVITY_FILLGravity used to fill the TabLayout as much as possible.

GRAVITY_FILL重力用于尽可能多地填充 TabLayout。

MODE_FIXEDFixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.

MODE_FIXED固定选项卡同时显示所有选项卡,最好与受益于选项卡之间快速旋转的内容一起使用。

MODE_SCROLLABLEScrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs.

MODE_SCROLLABLE可滚动选项卡在任何给定时刻显示选项卡的子集,并且可以包含更长的选项卡标签和更多的选项卡。

Set this in your code or your layout xml.

在您的代码或布局 xml 中进行设置。

app:tabGravity="center"
app:tabMode="fixed"

or

或者

tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
tabLayout.setTabMode(TabLayout.MODE_FIXED);    

Generally, using the code like blow can work without setting tabGravityand tabMode.

一般来说,使用像blow这样的代码可以不用设置tabGravitytabMode

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

回答by rpcao

Try adding this to MainActivity:

尝试将其添加到 MainActivity:

tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

回答by Fenil

Add - app:tabGravity="fill" property in the xml.

在 xml 中添加 - app:tabGravity="fill" 属性。

For Example -

例如 -

<android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill" />

回答by Mobi10

try this it works

试试这个它有效

 app:tabGravity="fill"
 app:tabMode="fixed"

回答by Parth Anjaria

Simple answer which I got from here.

我从这里得到的简单答案。

You just put this in your xml code :

您只需将其放入您的 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" />

回答by HaSeeB MiR

Set TabLayout width to match Parent

设置 TabLayout 宽度以匹配父级

Like this.

像这样。

<TabLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"/>