我在一个片段中有一个带有 TabLayout 的 AppBarLayout ,该片段位于具有工具栏的 Activity 中。但是toolbar和TabLayout之间出现了一个空格,我不知道它来自哪里。
片段包.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;
}
}
}
In your CoordinatorLayout
代替
android:fitsSystemWindows="true"
apply
android:fitsSystemWindows="false"
这里有一个很好的文档 https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec为什么以及何时应该使用android:fitsSystemWindows
系统窗口是屏幕上系统绘制非交互式的部分(在status bar
)或交互式(在navigation bar
) 内容。
大多数时候,您的应用程序不需要在status bar
or the navigation bar
,但如果您这样做:您需要确保交互元素(如按钮)没有隐藏在它们下面。这就是默认行为android:fitsSystemWindows=“true”
属性给你:它设置padding
视图以确保内容不会覆盖系统窗口。
需要记住以下几点:
1)fitsSystemWindows
应用深度优先 — 排序很重要:第一个消耗插图的视图会产生影响
2) 插图总是相对于整个窗口 ——甚至在布局发生之前就可以应用插图,所以不要假设默认行为在应用 View 时知道有关 View 位置的任何信息。padding
3) 任何其他padding
您设置的内容将被覆盖 — 您会注意到paddingLeft/paddingTop/
如果您正在使用等则无效android:fitsSystemWindows=”true”
在同一个视图上
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)