当CollapsingToolbarLayout折叠时显示工具栏图标

时间:2019-04-07 08:44:36

标签: android android-collapsingtoolbarlayout

我在我的应用程序中使用了CollapsingToolbarLayout,除了CollapsingToolbarLayout折叠时显示工具栏图标外,其他一切工作都很好,这些菜单项在CollapsingToolbarLayout展开时可以正确显示。但是当CollapsingToolbarLayout崩溃时会隐藏起来。我想向他们展示即使它处于折叠位置。

Expanded Image

Collapse Image

我的xml ....

<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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.ViewDetailsActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/MyAppbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_180sdp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/material_deep_teal_500"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewPager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="pin" />

                <com.tbuonomo.viewpagerdotsindicator.DotsIndicator
                    android:id="@+id/dots_indicator"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:gravity="bottom|center"
                    android:padding="@dimen/_8sdp"
                    app:dotsColor="@color/white"
                    app:dotsCornerRadius="@dimen/_6sdp"
                    app:dotsSize="@dimen/_8sdp"
                    app:dotsSpacing="@dimen/_2sdp"
                    app:dotsWidthFactor="2"
                    app:progressMode="true" />

            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/MyToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="parallax" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:background="#ffffff"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/fullLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="@dimen/_4sdp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:padding="@dimen/_2sdp"
                android:weightSum="3">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="bottom">

                    <TextView
                        android:id="@+id/priceTxt"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:fontFamily="@font/quicksand_medium"
                        android:gravity="bottom"
                        android:textColor="@color/black"
                        android:textSize="@dimen/_16ssp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:gravity="bottom">

                    <TextView
                        android:id="@+id/bedTxt"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="bottom"
                        android:textColor="@color/black"
                        android:textSize="@dimen/_12ssp" />

                </LinearLayout>

            </LinearLayout>

            <TextView
                android:id="@+id/typeTxt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="@dimen/_2sdp"
                android:paddingTop="@dimen/_1sdp"
                android:paddingEnd="@dimen/_2sdp"
                android:paddingBottom="@dimen/_1sdp"
                android:textColor="@color/black"
                android:textSize="@dimen/_12ssp" />

            <TextView
                android:id="@+id/addressTxt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="@dimen/_2sdp"
                android:paddingTop="@dimen/_1sdp"
                android:paddingEnd="@dimen/_2sdp"
                android:paddingBottom="@dimen/_2sdp"
                android:textSize="@dimen/_12ssp" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_5sdp">

                <com.github.ybq.android.spinkit.SpinKitView xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/details_spin_kit"
                    style="@style/SpinKitView.FadingCircle"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_40sdp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:padding="@dimen/_5sdp"
                    app:SpinKit_Color="@color/colorAccent" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/raleway"
                        android:text="Facts"
                        android:textColor="@color/black"
                        android:textSize="@dimen/_16ssp"
                        android:textStyle="bold"
                        tools:fontFamily="@font/raleway" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_4sdp"
                        android:baselineAligned="false"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:orientation="vertical">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:text="Reference"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:text="RERA permit No."
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:text="Type"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:text="Bedrooms"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:text="Bathrooms"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:text="Price"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp" />

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:text="Area"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp" />

                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:orientation="vertical">

                            <TextView
                                android:id="@+id/txtRef"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/txtRera"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/txtType"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/txtBedRooms"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/txtBathrooms"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/txtPrice"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/txtArea"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/raleway"
                                android:padding="@dimen/_2sdp"
                                android:textColor="@color/black"
                                android:textSize="@dimen/_12ssp"
                                android:textStyle="bold" />

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_5sdp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/raleway"
                    android:text="Amenities"
                    android:textColor="@color/black"
                    android:textSize="@dimen/_16ssp"
                    android:textStyle="bold"
                    tools:fontFamily="@font/raleway" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/amenityRecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_2sdp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_5sdp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/raleway"
                    android:text="About the area"
                    android:textColor="@color/black"
                    android:textSize="@dimen/_16ssp"
                    android:textStyle="bold"
                    tools:fontFamily="@font/raleway" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_4sdp"
                    android:orientation="horizontal"
                    android:weightSum="2">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/raleway"
                            android:padding="@dimen/_2sdp"
                            android:text="Region"
                            android:textColor="@color/black"
                            android:textSize="@dimen/_12ssp" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/raleway"
                            android:padding="@dimen/_2sdp"
                            android:text="City"
                            android:textColor="@color/black"
                            android:textSize="@dimen/_12ssp" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/raleway"
                            android:padding="@dimen/_2sdp"
                            android:text="District"
                            android:textColor="@color/black"
                            android:textSize="@dimen/_12ssp" />


                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/txtCity"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/raleway"
                            android:padding="@dimen/_2sdp"
                            android:textColor="@color/black"
                            android:textSize="@dimen/_12ssp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/txtDistrict"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/raleway"
                            android:padding="@dimen/_2sdp"
                            android:textColor="@color/black"
                            android:textSize="@dimen/_12ssp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/txtProvince"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/raleway"
                            android:padding="@dimen/_2sdp"
                            android:textColor="@color/black"
                            android:textSize="@dimen/_12ssp"
                            android:textStyle="bold" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_marginBottom="@dimen/_100sdp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/raleway"
                    android:text="Property description"
                    android:textColor="@color/black"
                    android:textSize="@dimen/_16ssp"
                    android:textStyle="bold"
                    tools:fontFamily="@font/raleway" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/_4sdp">

                    <WebView
                        android:id="@+id/descriptionWebView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <LinearLayout
        android:id="@+id/commLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@drawable/layout_shadow"
        android:gravity="bottom"
        android:weightSum="3">

        <LinearLayout
            android:id="@+id/layoutEmail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/_2sdp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="@dimen/_2sdp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="@dimen/_20sdp"
                android:src="@drawable/email" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="EMAIL"
                android:textColor="#F84242"
                android:textSize="@dimen/_8ssp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutSms"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/_2sdp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="@dimen/_2sdp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="@dimen/_20sdp"
                android:src="@drawable/sms" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="SMS"
                android:textColor="#F84242"
                android:textSize="@dimen/_8ssp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutCaller"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/_2sdp"
            android:layout_weight="1"
            android:autoLink="phone"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="@dimen/_2sdp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="@dimen/_20sdp"
                android:src="@drawable/call" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="CALL"
                android:textColor="#F84242"
                android:textSize="@dimen/_8ssp" />

        </LinearLayout>

    </LinearLayout>

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

1 个答案:

答案 0 :(得分:1)

将此行添加到工具栏

 app:layout_collapseMode="pin"

您的工具栏应如下所示:

 <android.support.v7.widget.Toolbar
                android:id="@+id/MyToolbar"
                app:layout_collapseMode="pin"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="parallax" />