简体   繁体   中英

How to make a collapsing toolbar in android?

so im trying to implement a simple collapsing toolbar, but I don't think my xml layout is correct. The text is appearing the image and when I scroll down, I can't scroll back up to see the image. Hope you guys can help!

<?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:ignore="RtlHardcoded">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/cardInfo_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/cardInfo_collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/image_kyoto"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello there"
                android:textSize="30dp"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello there"
                android:textSize="30dp"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello there"
                android:textSize="30dp"/>        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello there"
            android:textSize="30dp"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

Here's the image在此处输入图片说明

Your ImageView should have attribute called

app:layout_collapseMode="parallax"

If you want to add Toolbar to your Layout there should be attribute called

app:layout_collapseMode="pin"

And finally You missed one attribute for NestedScrollView.

app:layout_behavior="@string/appbar_scrolling_view_behavior"

in your ImageView, add the following attribute:

app:layout_collapseMode="parallax"

Also, in your NestedScrollView tag, add the following:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM