[英]Remove Space Gap between toolbar and tablayout
我有一个带有TabLayout的AppBarLayout,该片段位于具有工具栏的Activity中。 但是工具栏和TabLayout之间出现了一个空格,我不知道它来自哪里。
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;
}
}
}
在你的CoordinatorLayout
代替
android:fitsSystemWindows="true"
应用
android:fitsSystemWindows="false"
这是一个很好的文档,为什么以及何时应该使用android:fitsSystemWindows
系统窗口是屏幕的一部分,系统绘制的是非交互式(在status bar
的情况下)或交互式(在navigation bar
的情况下)内容。
大多数情况下,您的应用程序不需要在status bar
或navigation bar
下绘制,但如果您这样做:您需要确保交互式元素(如按钮)不会隐藏在它们下面。 这就是android:fitsSystemWindows=“true”
属性的默认行为为您提供:它设置View的padding
以确保内容不会覆盖系统窗口。
要记住以下几点:
1) fitsSystemWindows
应用深度优先 - 排序问题:它是第一个消耗fitsSystemWindows
的视图
2)Insets始终相对于整个窗口 - 即使在布局发生之前也可以应用insets,因此不要假设默认行为在应用其padding
时知道View的位置
3)你设置的任何其他padding
都被覆盖 - 如果你在同一个视图上使用android:fitsSystemWindows=”true”
,你会注意到paddingLeft/paddingTop/
etc是无效的
当您使用抽屉式导航,有app_bar_main.xml其中AppBarLayout已经存在,所以从app_bar_main.xml同时去除AppBarLayout和工具栏将解决这个问题。
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
在app_bar_main.xml和content_main.xml中, AppBarLayout和工具栏都存在。 从一个删除将解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.