如何设置Collapsing ToolBar高度

Par*_*dha 6 android android-viewpager android-collapsingtoolbarlayout

我正在使用Collapse ToolBar来进行如下布局.但ToolBar设置不正确.现在我将ToolBar高度设置为100dp.如果我减少它,设置选项卡的ViewPager将重叠在ToolBar上.

在此输入图像描述

这是我的xml.

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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="false">

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


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


                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/slide_image"
                    android:background="@drawable/slide_transparent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />

                <TextView
                    android:id="@+id/dashboard_txt_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="60dp"
                    android:gravity="center|bottom"
                    android:text="Salad"
                    android:textColor="@color/white"
                    android:textSize="25dp"
                    android:textStyle="italic" />

                <TextView
                    android:id="@+id/dashboard_txt_des"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="100dp"
                    android:gravity="center|bottom"
                    android:text="@string/welcome_des"
                    android:textColor="@color/white"
                    android:textSize="15dp" />


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:gravity="top"
                    android:minHeight="?attr/actionBarSize"
                    android:background="@drawable/toolbar_layout"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:titleMarginTop="15dp"
                    app:titleTextAppearance="@style/Toolbar.TitleText" />


                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="bottom"
                    android:background="@color/colorPrimary"
                    app:tabIndicatorColor="@color/white" />


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


            <!--<include layout="@layout/activity_collapse_toolbar" />-->
        </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.CoordinatorLayout>

    <!--<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />-->
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

更新:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="false">

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


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


                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/collapse_toolbar"
                    android:foreground="@drawable/slide_transparent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/slide_image"
                    app:layout_collapseMode="parallax" />

                <TextView
                    android:id="@+id/dashboard_txt_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/header"
                    android:layout_marginTop="60dp"
                    android:gravity="center|bottom"
                    android:text="Salad"
                    android:textColor="@color/white"
                    android:textSize="25dp"
                    android:textStyle="italic" />

                <TextView
                    android:id="@+id/dashboard_txt_des"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="100dp"
                    android:layout_below="@+id/dashboard_txt_title"
                    android:gravity="center|bottom"
                    android:text="@string/welcome_des"
                    android:textColor="@color/white"
                    android:textSize="15dp" />


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:layout_below="@+id/dashboard_txt_des"
                    android:background="@drawable/toolbar_layout"
                    android:gravity="top"
                    android:minHeight="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:titleMarginTop="15dp"
                    app:titleTextAppearance="@style/Toolbar.TitleText" />


                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_below="@+id/toolbar"
                    android:layout_gravity="bottom"
                    android:background="@color/colorPrimary"
                    app:tabIndicatorColor="@color/white" />


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


            <!--<include layout="@layout/activity_collapse_toolbar" />-->
        </android.support.design.widget.AppBarLayout>

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

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

    <!--<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />-->
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

期待结果:在此输入图像描述

sos*_*ite 6

也许这是一个旧答案,但有两个简单的解决方案:

1.Toolbarlayout_marginBottom

如果您需要透明(或半透明),TabLayout那么您可以执行以下操作:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">

        <YourMagicViewWithBackgroundImageTextAndOtherStuff
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="48dp"
            android:background="@color/semiTransparentHeaderBg"
            app:layout_collapseMode="pin" />

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_gravity="bottom"
            android:background="@color/semiTransparentHeaderBg" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

2.TabLayout外面CollapsingToolbarLayout

如果你不需要透明的,TabLayout那么就CollapsingToolbarLayout按照克里斯·拉森冬天的建议把它们放在外面。AppBarLayout扩展LinearLayout所以一切都应该完美无缺。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">

        <YourMagicViewWithBackgroundImageTextAndOtherStuff
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/semiTransparentHeaderBg"
            app:layout_collapseMode="pin" />

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

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)


sas*_*mar 1

在您的 CollapsingToolbarLayout 中设置每个 ImageView、TextView

android:layout_below="@+id/yourid "

根据您的设计视图顺序,将每个小部件一一设置在其他小部件下方。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">

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


        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
          <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="100dp"

                android:background="@drawable/toolbar_layout"
                android:gravity="top"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:titleMarginTop="15dp"
                app:titleTextAppearance="@style/Toolbar.TitleText" />

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
               android:layout_below="@+id/toolbar"
                android:foreground="@drawable/slide_transparent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/slide_image"
                app:layout_collapseMode="parallax" />

            <TextView
                android:id="@+id/dashboard_txt_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:layout_marginTop="60dp"
                android:gravity="center|bottom"
                android:text="Salad"
                android:textColor="@color/white"
                android:textSize="25dp"
                android:textStyle="italic" />

            <TextView
                android:id="@+id/dashboard_txt_des"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 android:layout_below="@+id/header"
                android:layout_marginTop="40dp"
                android:layout_below="@+id/dashboard_txt_title"
                android:gravity="center|bottom"
                android:text="@string/welcome_des"
                android:textColor="@color/white"
                android:textSize="15dp" />





            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_below="@+id/dashboard_txt_des"
                android:layout_gravity="bottom"
                android:background="@color/colorPrimary"
                app:tabIndicatorColor="@color/white" />


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


        <!--<include layout="@layout/activity_collapse_toolbar" />-->
    </android.support.design.widget.AppBarLayout>

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

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

<!--<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />-->
Run Code Online (Sandbox Code Playgroud)