加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 删除工具栏和tablayout之间的空间间隔

发布时间:2020-12-15 04:38:16 所属栏目:Java 来源:网络整理
导读:我有一个带有TabLayout的AppBarLayout,该片段位于具有工具栏的Activity中.但是工具栏和TabLayout之间出现了一个空格,我不知道它来自哪里. fragment_packs.xml FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://sc
我有一个带有TabLayout的AppBarLayout,该片段位于具有工具栏的Activity中.但是工具栏和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;
        }

    }

}

解决方法

在你的CoordinatorLayout中

代替

android:fitsSystemWindows="true"

应用

android:fitsSystemWindows="false"

这是一个很好的Documentation为什么以及何时应该使用android:fitsSystemWindows

系统窗口是屏幕的一部分,系统绘制的是非交互式(在状态栏的情况下)或交互式(在导航栏的情况下)内容.

大多数情况下,您的应用程序不需要在状态栏或导航栏下绘制,但如果您这样做:您需要确保交互式元素(如按钮)不会隐藏在它们下面.这就是android:fitsSystemWindows =“true”属性的默认行为为您提供:它设置View的填充以确保内容不会覆盖系统窗口.

要记住以下几点:

1)fitsSystemWindows应用深度优先 – 排序问题:它是第一个消耗插件的视图

2)Insets始终相对于整个窗口 – 即使在布局发生之前也可以应用insets,因此不要假设默认行为在应用其填充时知道View的位置

3)你设置的任何其他填充都被覆盖 – 如果你在同一个视图上使用android:fitsSystemWindows =“true”,你会注意到paddingLeft / paddingTop / etc是无效的

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读