我正在使用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应用中,谢谢?
答案 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);
}
}
请注意,这已将参数positionOffset
从int
更改为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>
我不知道其他任何可能出错的地方。