移除工具栏和选项卡之间的空白间隙

4
我有一个带有TabLayout的AppBarLayout,在一个带有Toolbar的Activity中的fragment中使用。但是在Toolbar和TabLayout之间出现了空白,我不知道它是从哪里来的。
请看下面的图片: enter image description here 这是我的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;
        }

    }

}

你不需要一个FrameLayout。将CoordinatorLayout移动到顶层元素。 - OneCricketeer
问题存在于Activity XML中,请在其中添加。 - OneCricketeer
CoordinatorLayout 添加 android:fitsSystemWindows="false" - rafsanahmad007
@rafsanahmad007 非常感谢!只需设置 fitsSystemWindows="false" - Kelton Holanda Fontenele
好的,我将把它翻译成中文作为答案返回... - rafsanahmad007
2个回答

12
在您的 CoordinatorLayout 中,不要使用
android:fitsSystemWindows="true"

申请

android:fitsSystemWindows="false"

这里是一个非常好的文档,介绍何时以及为什么要使用android:fitsSystemWindows

系统窗口是系统绘制非交互式内容(例如状态栏)或交互式内容(例如导航栏)的屏幕部分。

大多数情况下,你的应用程序不需要在状态栏或导航栏下面绘制东西,但如果确实需要,你需要确保交互元素(例如按钮)不会被它们遮挡。这就是android:fitsSystemWindows="true"属性的默认行为:它设置视图的padding以确保内容不会重叠到系统窗口上。

需要记住几件事:

1) fitsSystemWindows深度优先应用 - 排序很重要:第一个消耗插页的视图是起作用的

2) 插页总是相对于整个窗口的 - 在布局发生之前可能会应用插页,因此不要假设默认行为知道应用其padding时视图的位置。

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


0
当您使用导航抽屉时,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.xmlcontent_main.xml文件中,都存在AppBarLayoutToolbar组件。 从其中一个文件中移除即可解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接