Kel*_*ele 4 java xml android android-layout android-fragments
我有一个带有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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在你的 CoordinatorLayout
代替
android:fitsSystemWindows="true"
Run Code Online (Sandbox Code Playgroud)
应用
android:fitsSystemWindows="false"
Run Code Online (Sandbox Code Playgroud)
这是一个很好的文档,为什么以及何时应该使用android:fitsSystemWindows
系统窗口是屏幕的一部分,系统绘制的内容是非交互式(在情况下status bar
)或交互式(在内容的情况下navigation bar
).
大多数情况下,您的应用程序不需要在status bar
或下面绘制navigation bar
,但如果您这样做:您需要确保交互式元素(如按钮)不会隐藏在它们下面.这就是android:fitsSystemWindows=“true”
属性的默认行为为您提供的内容:它设置padding
了View以确保内容不会覆盖系统窗口.
要记住以下几点:
1)fitsSystemWindows
应用深度优先 - 有序的事情:它是第一个消耗有所作为的插图的视图
2)Insets始终相对于整个窗口 - 即使在布局发生之前也可以应用插入,因此不要假设默认行为在应用视图时知道View的位置. padding
3)padding
你设置的任何其他内容都被覆盖 - paddingLeft/paddingTop/
如果你android:fitsSystemWindows=”true”
在同一个视图上使用,你会注意到等等是无效的
归档时间: |
|
查看次数: |
1889 次 |
最近记录: |