12

我正在使用android支持设计tablayout。这是我的代码:

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

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

我的问题是标签总是左对齐。但是,我想将选定的选项卡居中(即使在开始时,第一个(选定的)选项卡也应该居中)。有没有办法做到这一点?谢谢。

4

4 回答 4

22

我查看了 TabLayout 和 tabContentStart 只为其第一个孩子设置填充 - > SlidingTabStrip,所以我在两侧手动设置:

public class CenteringTabLayout extends TabLayout {
    public CenteringTabLayout(Context context) {
        super(context);
    }

    public CenteringTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CenteringTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        View firstTab = ((ViewGroup)getChildAt(0)).getChildAt(0);
        View lastTab = ((ViewGroup)getChildAt(0)).getChildAt(((ViewGroup)getChildAt(0)).getChildCount()-1);
        ViewCompat.setPaddingRelative(getChildAt(0), (getWidth()/2) - (firstTab.getWidth()/2),0,(getWidth()/2) - (lastTab.getWidth()/2),0);
    }
}

TabLayout 的第一个 0 索引子项是 SlidingTabStrip。

于 2016-04-27T09:53:29.987 回答
5
public class MyTabLayout extends TabLayout {

    public MyTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTabMode(MODE_SCROLLABLE);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!changed) return;
        int totalTabsWidth = 0;
        for (int i = 0; i < getTabCount(); i++)
            totalTabsWidth += ((ViewGroup) getChildAt(0)).getChildAt(i).getWidth();
        int padding = (getWidth() - totalTabsWidth) / 2;
        if (padding < 0) padding = 0;
        getChildAt(0).setPaddingRelative(padding, 0, padding, 0);
    }
}
于 2018-01-28T03:08:24.420 回答
5

Perhaps you are looking for an xml attribute called app:tabContentStart. This attribute allows you push the active tab to the left according to the value specified, which is set in "dp" (kind of like a margin-left thing). For example, try putting your code like this:

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        app:tabContentStart="96dp"/> <-- Or whatever value you want here.

It doesn't exactly center the tab, but it gives you a similar effect as the Google Play Store tabs.

于 2015-11-06T08:47:37.827 回答
2

如果您的目标 API 是 23 或更高版本,让我再分享一个选项以供考虑,以及app:tabContentStartSynerFlow 建议的配置,它可以让末端 Tab 在滚动期间居中而不是滑动到最左边。但是,我仍在研究 23 岁之前的 API 方法。

final int TAB_CONTENT_START = 96;   

tabLayout.setOnScrollChangeListener(new View.OnScrollChangeListener() {

    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        final int maxAllowWidthOfTabBar = tabLayout.getMeasuredWidth() + TAB_CONTENT_START;
        if (scrollX > maxAllowWidthOfTabBar)
            tabLayout.setScrollX(maxAllowWidthOfTabBar);

    }
});
于 2017-01-19T08:29:32.933 回答