Java – removes the space between the toolbar and tabrayout
I have an appbarlayout with tablayout, which is located in the activity with toolbar But there is a space between the toolbar and tablayout. I don't know where it comes from
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; } } }
Solution
In your coordinatorlayout
replace
android:fitsSystemWindows="true"
application
android:fitsSystemWindows="false"
This is a good documentation of why and when Android: fitssystemwindows should be used
The system window is a part of the screen. The system draws non interactive (in the case of the status bar) or interactive (in the case of the navigation bar)
In most cases, your application does not need to draw under the status bar or navigation bar, but if you do: you need to ensure that interactive elements such as buttons are not hidden under them This is the default behavior of the Android: fitssystemwindows = "true" attribute, which sets the padding of the view to ensure that the content does not overwrite the system window
Remember the following:
1) Fitssystemwindows application depth first - sorting problem: it is the first view to consume plug-ins
2) Insets are always relative to the entire window – insets can be applied even before the layout occurs, so don't assume that the default behavior knows the position of the view when its fill is applied
3) Any other padding you set is overwritten - if you use Android: fitssystemwindows = "true" on the same view, you will notice that paddingleft / paddingtop / etc is invalid