删除工具栏和选项卡布局之间的空格 [英] Remove Space Gap between toolbar and tablayout

查看:81
本文介绍了删除工具栏和选项卡布局之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个带有工具栏的Activity中的片段中有一个带有TabLayout的AppBarLayout.但是在工具栏和TabLayout之间出现一个空格,我不知道它来自哪里.

I have a AppBarLayout with TabLayout in a fragment that is into an Activity that has a Toolbar. But between toolbar and TabLayout appears a space, i don't know where it comes from.

fragment_packs.xml

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>

PackagesFragment.java

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

Here is a good Documentation why and when you should use a android:fitsSystemWindows

系统窗口是系统绘制非交互式(对于status bar而言)还是交互式(对于navigation bar而言)内容的屏幕部分.

System windows are the parts of the screen where the system is drawing either non-interactive (in the case of the status bar) or interactive (in the case of the navigation bar) content.

大多数情况下,您的应用无需在status barnavigation bar下绘制,但是如果这样做,则需要确保交互元素(如按钮)没有隐藏在它们的下方.这就是android:fitsSystemWindows=true属性的默认行为所提供的:它设置了View的padding以确保内容不会覆盖系统窗口.

Most of the time, your app won’t need to draw under the status bar or the navigation bar, but if you do: you need to make sure interactive elements (like buttons) aren’t hidden underneath them. That’s what the default behavior of the android:fitsSystemWindows="true" attribute gives you: it sets the padding of the View to ensure the contents don’t overlay the system windows.

请记住以下几点:

1)fitsSystemWindows首先应用深度-顺序很重要:这是第一个使用嵌入的图片产生差异的视图

1)fitsSystemWindows is applied depth first — ordering matters: it’s the first View that consumes the insets that makes a difference

2)插入总是相对于整个窗口的.—插入可能甚至在布局发生之前就被应用,因此不要假定默认行为在应用视图padding

2) Insets are always relative to the full window — insets may be applied even before layout happens, so don’t assume the default behavior knows anything about the position of a View when applying its padding

3)您设置的其他任何padding都会被覆盖—您会注意到,如果您在同一视图上使用android:fitsSystemWindows=true,则paddingLeft/paddingTop/等无效.

3) Any other padding you’ve set is overwritten — you’ll note that paddingLeft/paddingTop/ etc is ineffective if you are using android:fitsSystemWindows="true" on the same View

这篇关于删除工具栏和选项卡布局之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆