android - 使用折叠工具栏和选项卡滚动

标签 android android-layout android-toolbar android-coordinatorlayout

我正在尝试使用具有 scroll|exitUntilCollapsed 标志的 CollapsingToolbarLayout 和具有 TabLayout 进行布局scroll|enterAlways scrollFlag 属性。基本上我希望我的工具栏被固定并在滚动时显示和隐藏选项卡。我从 https://github.com/chrisbanes/cheesesquare 修改了 cheesesquare 应用程序.这是我的布局 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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_discuss"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>

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

这是结果;

选项卡未正确定位。他们不关心 enterAlways 属性。

enter image description here

最佳答案

this可能会解决您的问题。

只需将 android:layout_gravity="bottom" 添加到 tablayout 并将 android:gravity="top" 添加到工具栏。

enter image description here

关于android - 使用折叠工具栏和选项卡滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32865957/

相关文章:

Android NDK - 尝试用 NoMemoryError 捕获

android - 如何在栏顶部显示不同的值

android - 在 Android 4.4 KitKat 上应用 Material 设计

android - 如何在 Android 中动态合并两个 Drawable?

android - 工具栏菜单图标不显示

Android工具栏溢出菜单更改文本颜色并将菜单填充到屏幕

java - 如何从底部导航栏打开本身位于 fragment 中的 fragment ?

java - Android 布局编辑器中的 NotFoundException?

android - EditText 中缺少光标和下划线

android - 有没有办法在android中产生特定声压级的纯音?