我已经设置选项卡为UPDATE 29/05/2015这篇文章。在我的Nexus 4手机上,标签是全宽的,但在Nexus 7平板电脑上,它位于中间,没有覆盖全屏宽度。
Nexus 7截图 Nexus 4截图
我已经设置选项卡为UPDATE 29/05/2015这篇文章。在我的Nexus 4手机上,标签是全宽的,但在Nexus 7平板电脑上,它位于中间,没有覆盖全屏宽度。
Nexus 7截图 Nexus 4截图
当前回答
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed" />
为我工作。这里还有xmlns:app="http://schemas.android.com/apk/res-auto"
其他回答
从Kaizie那里借用的一个“更简单”的答案只是在你的TabLayout xml中添加app:tabMaxWidth="0dp":
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed" />
为什么要这么忙? 只要把app:tabMode="scrollable"放在你的XML TabLayout中。 这是它。
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed" />
为我工作。这里还有xmlns:app="http://schemas.android.com/apk/res-auto"
对我来说,下面的代码可以工作并且足够了。
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"/>
在我的这个问题的变体中,我有3个中等大小的标签,它们没有占据平板电脑的全部宽度。我不需要在平板电脑上滚动标签,因为平板电脑足够大,可以在不滚动的情况下显示所有标签。但我确实需要这些标签可以在手机上滚动,因为手机太小了,无法同时显示所有的标签。
在我的案例中,最好的解决方案是添加一个res/layout-sw600dp/main_activity.xml文件,其中相关的TabLayout可以有app:tabGravity="fill"和app:tabMode="fixed"。但在我的常规res/layout/main_activity.xml中,我省略了app:tabGravity="fill"和app:tabMode="fixed",而使用app:tabMode="scrollable"。