5

我在 中添加 2 个选项卡TabLayout,以下是我的代码。

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tabLayout"
    style="@style/AppTabLayout"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_gravity="center"
    app:tabBackground="@drawable/tab_selector_statistics"
    android:background="@drawable/tab_statistics"
    app:tabContentStart="24dp"
    app:tabGravity="fill"
    app:tabMode="fixed"
    app:tabSelectedTextColor="@color/white"
    app:tabTextColor="@color/black"
    app:layout_constraintBottom_toBottomOf="@+id/view5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/view5"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true" />

我得到以下输出。

在此处输入图像描述

但我希望标签占据屏幕的整个宽度,如下所示。

在此处输入图像描述

以下是我在文件中AppTabLayout定义的样式。styles.xml

<style name="AppTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@null</item>
    <item name="tabIndicatorHeight">1dp</item>
    <item name="tabPaddingStart">16dp</item>
    <item name="tabPaddingEnd">16dp</item>
    <item name="tabSelectedTextColor">@color/white</item>
</style>
4

7 回答 7

10

只需添加以下内容。

            android:layout_width="match_parent"
            android:layout_height="30dp"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabMode="fixed"
于 2019-02-17T10:10:35.340 回答
1

使用风格

<style name="MyTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabGravity">fill</item>
    <item name="tabMaxWidth">0dp</item>
</style>

或者

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

参考

全宽表格布局

于 2019-02-23T09:58:22.097 回答
0

您可能还想app:tabIndicatorFullWidth="true"TabLayout

于 2021-06-24T16:39:20.910 回答
0

ConstraintLayout 不支持 match_parent。将宽度设置为 0dp 以使其匹配约束。

android:layout_width="0dp"

来自官方文档

重要提示:不建议将 MATCH_PARENT 用于 ConstraintLayout 中包含的小部件。可以通过使用 MATCH_CONSTRAINT 来定义类似的行为,并将相应的左/右或上/下约束设置为“父”。

于 2019-02-23T07:19:25.007 回答
0

您应该在表格布局中使用stretchColumns

<TableLayout
        android:stretchColumns="*">
        <TableRow
            android:layout_width="0dp">
    <TableRow
            android:layout_width="0dp">
</TableLayout>
于 2019-02-23T09:43:36.200 回答
0

只需执行以下操作。这对我来说是工作。

<android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabBackground="@color/colorPrimary"
                app:tabIndicatorColor="@android:color/white"
                app:tabTextColor="@android:color/white"
                app:tabMode="fixed"
                app:tabGravity="fill"/>
于 2019-02-23T10:10:30.200 回答
-1
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="0dp"
        android:layout_height="34.5dp"
        android:elevation="@dimen/dimen_10"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/overTimeCV"
        app:tabMode="fixed"/>

与 viewPager 一起检查此 TabLayout

于 2019-02-26T06:49:15.123 回答