TabLayout 第一项向左偏移

TabLayout first item offset on the left

我有一个 TabLayout,带有使用 Material Design 准则状态的水平滚动选项卡。

On mobile and desktop, offset the first tab on the left to sign that tabs are scrollable.

我怎样才能做到这一点?如果我添加左边距或填充,这只会将所有选项卡移动到右侧,但我无法再将选项卡一直滚动到屏幕左侧。

我也试过这个:

tabLayout.setScrollPosition(2, 0.2f, true);

这会预先 select 第三个项目并将滑动指示器偏移 0.2,这最终看起来很奇怪而且实际上也没有将第一个选项卡项目移动到屏幕的右侧。

提前感谢您的帮助!

我找到了解决方案。在布局 XML 中,您可以使用 "tabContentStart".

设置内容偏移量

示例:

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabContentStart="57dp"
        app:tabMode="scrollable" />

这是快乐而美好的 Jetpack Compose 世界中的解决方案:

ScrollableTabRow(
    selectedTabIndex = ...
    edgePadding = 0.dp
) { /* Your Tabs */ }