2016-01-19 42 views
1

我在跟随this tutorial,它解释了如何实现Google Design Support Library中包含的新TabLayout。这里是我的应用程序:使用设计支持库的选项卡不占用全宽

enter image description here

如可以在上面的截图中可以看出,我的选项卡不会拍摄画面的整个宽度。

我该如何解决?

这里是我的活动的xml: (我的Java代码是一样的一个在the tutorial在下面的代码我tablayout和viewpager是的FrameLayout里面。)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 
<!-- your content layout --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
    <!-- Toolbar instead of ActionBar so the drawer can slide on top --> 
    <!-- If you want to have dark background for options buttons remove the popup theme --> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/abc_action_bar_default_height_material" 
      android:background="?attr/colorPrimary" 
      android:minHeight="?attr/actionBarSize" /> 
    <!-- Real content goes here --> 
     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 
      <android.support.design.widget.TabLayout 
       android:id="@+id/sliding_tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:tabMode="scrollable" /> 
      <android.support.v4.view.ViewPager 
       android:id="@+id/viewpager" 
       android:layout_width="match_parent" 
       android:layout_height="0px" 
       android:layout_weight="1" 
       android:background="@android:color/white" /> 
     </FrameLayout> 
    </LinearLayout> 
    <android.support.design.widget.NavigationView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:id="@+id/nav_view" 
     app:headerLayout="@layout/nav_view_header" 
     app:menu="@layout/nav_view_menu" 
     app:theme="@style/MyTheme.NavMenu" /> 
</android.support.v4.widget.DrawerLayout> 

编辑:

后加入app:tabGravity="center",改变app:tabModefixed,这里的应用程序的外观:

enter image description here

我需要它们来填充宽度。

此问题不重复。另一个答案没有解决我的问题。

+0

的可能的复制[在TabLayout标签不填满整个动作条( http://stackoverflow.com/questions/30721498/tabs-in-tablayout-not-filling-up-entire-actionbar) – Mohsen

回答

2

http://developer.android.com/reference/android/support/design/widget/TabLayout.html

GRAVITY_CENTER重力用于布置在 TabLayout的中心的突出部。

GRAVITY_FILL重力用于尽可能地填充TabLayout。

MODE_FIXED固定选项卡同时显示所有选项卡,最好使用 ,内容受益于选项卡之间的快速旋转。

MODE_SCROLLABLE可滚动选项卡显示任何 给定时刻的选项卡子集,并且可以包含更长的选项卡标签和更多数量的选项卡。

只需设置这在您的TabLayout

app:tabMode="fixed" 

编辑:

我使用这个和它的工作:

   <android.support.design.widget.TabLayout 
       android:id="@+id/tab_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/toolbar" 
       android:background="?attr/colorPrimary" 
       android:minHeight="?attr/actionBarSize" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

好像你不”不需要设置任何东西。

证明:

enter image description here

顺便说一句,你也可以检查:

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 
+0

是的,你将不得不使用'gravity_center'! – zIronManBox

+0

这解决了他们在中心,但仍然不会采取全屏的屏幕... – VSG24

+0

删除该滚动应该工作,然后我猜:这行:'app:tabMode =“可滚动”' – Mohsen

-1

设置TabLayoutMode这样的:

tabLayout.setTabMode(TabLayout.MODE_FIXED); 
+1

这与设置'app:tabMode =“fixed”'我已经做了什么不同吗? – VSG24

0

解决的办法是把这些东西加到xml和java:

在Java:

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

以XML为TabLayout:

  app:tabGravity="center" 
      app:tabMode="fixed" 

结果:

enter image description here

+0

我的回答有什么不同? :) – Mohsen

+0

Java代码是必须的。重力需要在xml和java中设置。在xml它需要设置为'中心'和在Java中'填充' – VSG24

+0

你不会需要,我的答案正在working.without这两个! – Mohsen

相关问题