tablayout每个tab如何设置固定宽度?
时间: 2024-03-02 09:50:29 浏览: 375
要设置 `TabLayout` 中每个 `Tab` 的固定宽度,可以使用 `app:tabMode="fixed"` 属性,它会固定 `Tab` 的宽度并平分 `TabLayout` 的宽度。然后,可以使用 `app:tabMinWidth` 属性来设置 `Tab` 的最小宽度,所有的 `Tab` 宽度都会根据最小宽度进行自适应。
以下是一个使用 `TabLayout` 的示例:
```xml
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabMinWidth="0dp"
app:tabGravity="fill"/>
```
在布局文件中,我们将 `TabLayout` 的 `tabMode` 设置为 `fixed`,并将 `tabMinWidth` 设置为 `0dp`。这样就可以让每个 `Tab` 根据内容自适应宽度。
如果想固定每个 `Tab` 的宽度,可以将 `tabMinWidth` 设置为一个具体的值,例如:
```xml
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabMinWidth="80dp"
app:tabGravity="fill"/>
```
在这个示例中,我们将 `tabMinWidth` 设置为 `80dp`,这样每个 `Tab` 的宽度都将固定为 `80dp`。
阅读全文
相关推荐


















