【问题标题】:Remove Space Gap between toolbar and tablayout删除工具栏和选项卡布局之间的空间间隙
【发布时间】:2017-09-04 18:08:44
【问题描述】:

我在一个片段中有一个带有 TabLayout 的 AppBarLayout,该片段位于一个具有工具栏的 Activity 中。但是toolbar和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>

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 移动到顶部元素
  • 而且Activity XML中存在问题,请补充
  • 添加:android:fitsSystemWindows="false"' for the CoordinatorLayout`...
  • @rafsanahmad007 非常感谢!仅设置fitsSystemWindows="false"
  • 好的,我将其添加为答案...

标签: java android xml android-layout android-fragments


【解决方案1】:

当您使用导航抽屉时,有 app_bar_main.xml 其中 AppBarLayout 已经存在,因此从 app_bar_main.xml 中删除 AppBarLayout 和 Toolbar 将解决问题。

     <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 AppBarLayout 和 Toolbar 中都存在。 从一个中删除将解决问题

【讨论】:

    【解决方案2】:

    在你的CoordinatorLayout

    而不是

    android:fitsSystemWindows="true"
    

    申请

    android:fitsSystemWindows="false"
    

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

    系统窗口是系统正在绘制非交互式(在status bar 的情况下)或交互式(在navigation bar 的情况下)内容的屏幕部分。

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

    需要注意的几点:

    1)fitsSystemWindows 应用深度优先 — 排序很重要:它是第一个使用 insets 的 View 会产生影响

    2) 插入总是相对于整个窗口 — 插入甚至可以在布局发生之前应用,所以不要假设默认行为在应用其padding时知道任何关于视图位置的信息@

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

    【讨论】:

      猜你喜欢
      • 2018-01-05
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多