如何在Play Market中居中SlidingTabStrip?

时间:2015-02-27 19:34:18

标签: android tabs pagerslidingtabstrip

我正在使用https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java标签类。

将它应用到我的项目后,我得到(没有居中)标签,如图所示 this picture,但我希望集中我的标签,例如Play Market标签。

如何将我的标签集中在Google Play应用中,谢谢?

3 个答案:

答案 0 :(得分:1)

我花了一点时间在这上面,最近才开始工作。这是摘要。

<强> SlidingTabLayout.java 您必须修改scrollToTab方法以计算正确的滚动。我在这里添加了解释性评论:

private void scrollToTab(int tabIndex, float positionOffset) {
    final int tabStripChildCount = mTabStrip.getChildCount();
    if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
        return;
    }

    View selectedChild = mTabStrip.getChildAt(tabIndex);
    View nextChild = mTabStrip.getChildAt(tabIndex + 1);
    if (selectedChild != null) {
        // c1 is the center of the currently selected tab
        int c1 = selectedChild.getLeft() + (selectedChild.getWidth() / 2);

        // viewCenter is the center of the parent view
        int viewCenter = getWidth() / 2;

        int targetCenter = c1;
        if (nextChild != null) {
            // c2 is the center of the tab to the right of the currently selected
            int c2 = nextChild.getLeft() + (nextChild.getWidth() / 2);

            // the 'targetCenter' is partway between c1 & c2 depending on how far the viewpager
            // is currently scrolled
            targetCenter = (int) (c1 + ((c2 - c1) * positionOffset));
        }

        int targetScrollX = targetCenter - viewCenter;

        scrollTo(targetScrollX, 0);
    }
}

请注意,这已将参数positionOffsetint更改为float,这是故意的,因为我们无法再使用从{{1}传入的原始值{&#39} InternalViewPagerListener方法,因为它没有考虑下一个标签的宽度。这意味着在onPageScrolled方法中我们必须改变:

onPageScrolled

是:

        View selectedTitle = mTabStrip.getChildAt(position);
        int extraOffset = (selectedTitle != null)
                ? (int) (positionOffset * selectedTitle.getWidth())
                : 0;
        scrollToTab(position, extraOffset);

这使我们可以访问我们需要进行正确计算的原始 scrollToTab(position, positionOffset); 参数。

这些是唯一需要的修改。我在这里发布了全班的要点:https://gist.github.com/danh32/c39465c8b04db059fe72

答案 1 :(得分:0)

修改scrollToTab方法以使所选标签居中。

private void scrollToTab(int tabIndex, int positionOffset) {
    final int tabStripChildCount = mTabStrip.getChildCount();
    if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
        return;
    }

    View selectedChild = mTabStrip.getChildAt(tabIndex);
    if (selectedChild != null) {
        int viewWidth = getWidth();
        int tabWidth = selectedChild.getWidth();
        int tabPosition = selectedChild.getLeft() + positionOffset;
        int targetScrollX = tabPosition - viewWidth / 2 + tabWidth / 2;

        scrollTo(targetScrollX, 0);
    }
}

答案 2 :(得分:0)

您的 activity_main.xml 文件应该是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

<samples.exoguru.materialtabs.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:background="@color/ColorPrimary"/>

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

我不知道其他任何可能出错的地方。

enter image description here