【问题标题】:Avoid ViewPager going under Toolbar避免 ViewPager 进入工具栏
【发布时间】:2016-02-24 00:06:20
【问题描述】:

基于 this 回答我试图为我的用例制定解决方案。我没有使用折叠工具栏,而是使用常规工具栏,但使用TabLayout。但是,对于TabLayout,我需要使用ViewPager,但我不知道将其放置在何处以避免FrameLayout 出现问题。 ViewPager 在我的 Host Fragment 中,当它的布局替换 FrameLayout 时,由于某种原因,内容位于 Toolbar 和 TabLayout 下方...

我试着把它放在那条线下面

<include layout="@layout/content_main" />

the app_bar_main.xml,但它搞砸了我的用户界面。

真正的问题是,如果我的 Fragment 需要 FrameLayout,而 TabLayout 需要 ViewPager,我该如何实现解决方案?

您可以看到我稍微移动了视图,以便隐藏工具栏。这样,即使在工具栏下方(可见时),ViewPager 也会占用空间

当前尝试 - appbar_content(工具栏只是一个单独布局的工具栏):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout  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=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"

    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include layout="@layout/toolbar" />

    <android.support.design.widget.TabLayout
        android:visibility="visible"
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/teal"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/purple800"
        app:tabIndicatorHeight="4dp"
        app:itemIconTint="@color/tabicons"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/tealfifty"
        app:tabTextAppearance="@style/TabCapsFalse"
        app:tabTextColor="@color/iron" />
</android.support.design.widget.AppBarLayout>


<include layout="@layout/content_main" />



</android.support.design.widget.CoordinatorLayout>

content_main(删除了 NestedScrollView,因为它搞砸了一切,把我的整个片段视图放在屏幕中间的一个小框架(可能是 50x50 大小)中)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/content_main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />

我的DrawerLayout

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include
    layout="@layout/appbar_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:fitsSystemWindows="true"
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/tealfifty"
        app:itemBackground="@drawable/drawer_item"
        app:itemTextColor="@color/drawer_item"
        app:itemIconTint="@color/drawer_item"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/navigation_view_items" />
</android.support.v4.widget.DrawerLayout>

【问题讨论】:

    标签: android android-viewpager navigation-drawer


    【解决方案1】:

    经过一番挣扎,我发现在我的 FrameLayout 中放置一个 NestedScrollView 并为我的 FrameLayout 分配一个 layout_behaviour 使得 Scroll View 继承了它,现在这一切都像一个魅力!

    这是我的 DrawerLayout

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    
    <include
        layout="@layout/appbar_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
        <android.support.design.widget.NavigationView
            android:fitsSystemWindows="true"
            android:id="@+id/nvView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/tealfifty"
            app:itemBackground="@drawable/drawer_item"
            app:itemTextColor="@color/drawer_item"
            app:itemIconTint="@color/drawer_item"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/navigation_view_items" />
    </android.support.v4.widget.DrawerLayout>
    

    appbar_content.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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=".MainActivity">
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <include layout="@layout/toolbar" />
    
        <android.support.design.widget.TabLayout
            android:visibility="visible"
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/teal"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/purple800"
            app:tabIndicatorHeight="4dp"
            app:itemIconTint="@color/tabicons"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/tealfifty"
            app:tabTextAppearance="@style/TabCapsFalse"
            app:tabTextColor="@color/iron" />
    </android.support.design.widget.AppBarLayout>
    
    
    
    <FrameLayout android:id="@+id/content_main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:showIn="@layout/appbar_content"
        xmlns:tools="http://schemas.android.com/tools">
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/scroll_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>
    
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:visibility="invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/addico"
        app:layout_anchor="@id/content_main_frame"
        app:layout_anchorGravity="bottom|end" />
    

    【讨论】:

    • 我在使用 ViewPager 时遇到了同样的问题。将 layout_behaviour 属性添加到 ViewPager,解决了这个问题..
    • 很高兴你修好了 :)
    【解决方案2】:

    将您的选项卡布局和视图寻呼机放置在垂直方向的线性布局中。因此,您的线性布局将是 Coordinator 布局的子级。这将确保您的视图寻呼机从 tablayout 下方开始。

    【讨论】:

    • 我的 TabLayout 是 AppBarLayout 的一部分,因此当它折叠时它会随着工具栏一起移动。我的 ViewPager 甚至不在同一个布局中。 TabLayout 和 Toolbar 在 DrawerLayout 中,所以我认为您的建议无法实现?您看到我所说的作为解决方案遵循的答案了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 2016-10-26
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多