带有活动标签的Android TabLayout始终位于中心位置,就像在Play报亭应用中一样

时间:2015-10-17 21:30:18

标签: android material-design android-tablayout google-play-newsstand

android.support.design.widget.TabLayout中,如何使活动标签始终显示在中心,就像在 Play报亭应用中一样,如下所示。

第一个和最后一个标签也应出现在中心。

Play Newsstand

Play Newsstand

我尝试在TabLayout上使用填充。它不起作用。这是我写的代码:

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/yellow"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="#EEE"
            app:tabMode="scrollable"
            android:gravity="bottom"
            android:clipToPadding="false"
            android:paddingLeft="90dp"
            android:paddingRight="90dp"
            />

在这种情况下,tabIndicator也将从左侧移动90dp。它应该保持在独立于填充的中心。

2 个答案:

答案 0 :(得分:0)

查看此解决方案:enter image description here

可以通过tabContentStart解决,但是如果你想要将Tablayout的两边都居中,你需要扩展这个类并手动设置填充。

答案 1 :(得分:0)

也许有点晚了,但你可以尝试使用这个库:

https://github.com/jpardogo/PagerSlidingTabStrip

这可以按照他们的说法完成:

在build.gradle文件中包含以下依赖项。

compile 'com.jpardogo.materialtabstrip:library:1.1.1'

或者将库添加为项目。我尝试发送拉取请求,但看起来原始开发人员不再维护它了。

在布局中包含PagerSlidingTabStrip小部件。通常应将其放在它所代表的ViewPager上方。

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

在onCreate方法(或片段的onCreateView)中,将窗口小部件绑定到ViewPager:

// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new TestAdapter(getSupportFragmentManager()));

// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);

希望得到这个帮助。