2017-04-09 175 views
1

我有一个带有TabLayout的AppBarLayout,它位于具有工具栏的Activity中。但是在工具栏和TabLayout之间出现一个空格,我不知道它来自哪里。删除工具栏和tablayout之间的空白空间

enter image description here

fragment_packs.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="studio.com.archeagemanager.EventosFragment"> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context="studio.com.archeagemanager.PacksFragment"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:tabGravity="fill" 
       app:tabMode="fixed" 
       app:tabTextColor="#ffffff" /> 
     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/white" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    </android.support.design.widget.CoordinatorLayout> 

</FrameLayout> 

PacksFragment.java

public class PacksFragment extends Fragment { 


    public PacksFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_packs, container, false); 

     AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.appbar); 
     appBarLayout.setExpanded(false); 

     TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs); 

     final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager); 
     LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); 
     mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 

     viewPager.setAdapter(new PagerAdapter(getFragmentManager())); 
     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
     tabLayout.setupWithViewPager(viewPager); 
     tabLayout.setTabMode(TabLayout.MODE_FIXED); 
     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 

     return view; 
    } 

    public class PagerAdapter extends FragmentStatePagerAdapter { 

     private String[] tabTitles = new String[]{"Tab1", "Tab2", "Tab3", "Tab4"}; 

     public CharSequence getPageTitle(int position) { 
      return tabTitles[position]; 
     } 

     public PagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
       case 0: 
        return new TabFragmentA(); 
       case 1: 
        return new TabFragmentA(); 
       case 2: 
        return new TabFragmentA(); 
       case 3: 
        return new TabFragmentA(); 
       default: 
        return null; 
      } 
     } 

     @Override 
     public int getCount() { 
      return tabTitles.length; 
     } 

    } 

} 
+0

你不需要使用的FrameLayout android:fitsSystemWindows=”true”。移动CoordinatorLayout顶端元素 –

+0

而在活动XML存在问题,所以请添加 –

+0

地址:'机器人:fitsSystemWindows =“假””为'CoordinatorLayout' ... – rafsanahmad007

回答

3

在你CoordinatorLayout

,而不是

android:fitsSystemWindows="true" 

申请

android:fitsSystemWindows="false" 

这里是一个很好Documentation为什么以及何时应该使用android:fitsSystemWindows

系统窗口表示系统正在制定或者非交互式(的情况下,屏幕的部分的status bar)或交互式(在navigation bar的情况下)内容。

大多数情况下,您的应用程序不需要根据status barnavigation bar进行绘制,但是如果您这么做:您需要确保交互式元素(如按钮)不会隐藏在它们下面。这就是android:fitsSystemWindows=“true”属性的默认行为给你的:它设置视图的padding以确保内容不覆盖系统窗口。

有几件事情要记住:

1)fitsSystemWindows首先应用深度 - 排序问题:它是第一个观点,即消耗是有差别的插图

2)插图总是相对于全窗口 - 在申请时插图甚至可能布局发生之前被应用,所以不要以为默认行为,知道一个视图的位置的任何其padding

3)任何其他padding你设置被覆盖 - “ LL注意paddingLeft/paddingTop/等是无效的,如果你在同一视图