代码之家  ›  专栏  ›  技术社区  ›  dev90

如何在tablayout中设置2个等宽制表符

  •  0
  • dev90  · 技术社区  · 6 年前

    我在 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" />
    

    我得到以下输出。

    enter image description here

    enter image description here

    以下是我的 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>
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   Rezaul Karim    6 年前

    只需添加以下内容。如果有效的话,请告诉我并投票。

                android:layout_width="match_parent"
                android:layout_height="30dp"
                app:tabMaxWidth="0dp"
                app:tabGravity="fill"
                app:tabMode="fixed"
    
        2
  •  0
  •   Sharath kumar    6 年前

    ConstraintLayout不支持匹配父项。将width设置为0dp以使其与约束匹配。

    android:layout_width="0dp"
    

    official doc

    重要提示:对于包含在 约束布局。类似的行为可以通过使用 正在将约束设置为“父级”。

        3
  •  0
  •   Nikhil Vadoliya    6 年前

    应该在表布局中使用stretchColumns

    <TableLayout
            android:stretchColumns="*">
            <TableRow
                android:layout_width="0dp">
        <TableRow
                android:layout_width="0dp">
    </TableLayout>
    
        4
  •  0
  •   Vasudev Vyas    6 年前

    <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"/>
    
        5
  •  0
  •   Jitesh Prajapati ravi    6 年前

    使用样式

    <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" />
    

    参考

    Full width tablayout

        6
  •  -1
  •   Abraham Mathew    6 年前
        <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一起查看此表格