2017-04-09 241 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/等是無效的,如果你在同一視圖