滚动工具栏时阻止按钮隐藏

时间:2016-12-19 15:04:45

标签: android android-appbarlayout android-collapsingtoolbarlayout

我正在构建一个应用程序,我希望在一个Activity中拥有CollapsingToolbarLayout。一切正常,除了当我滚动到按钮的最上半部分时,工具栏下面是这样的:

enter image description here

我想要实现的是:

enter image description here

我的布局是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_game_info"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@android:color/transparent"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginBottom="-70dp"
            app:expandedTitleMarginEnd="64dp"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/imageDescTropos"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:scaleType="fitXY"
                android:contentDescription="Nothing"
                android:src="@drawable/download"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolBar"
                android:layout_width="match_parent"
                app:titleEnabled="false"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

        </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:background="@color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


            <TextView
                android:id="@+id/game_full_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="13.5dp"
                android:layout_marginLeft="16dp"
                android:scrollbars="vertical"
                android:text="Game Name"
                android:fadeScrollbars="true"
                android:fontFamily="sans-serif-black"
                android:textColor="@color/black"
                android:textSize="18sp"
                tools:targetApi="jelly_bean" />

            <TextView
                android:id="@+id/my_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Category"
                android:layout_marginLeft="16dp"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="9dp"
                android:background="@color/game_info_jackpot_background">

                <TextView
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif-light"
                    android:layout_marginTop="9.5dp"
                    android:layout_marginBottom="9dp"
                    android:layout_marginLeft="16dp"
                    android:id="@+id/textView" />
            </LinearLayout>

            <TextView
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer iaculis luctus elementum. Maecenas ultricies volutpat mollis. Pellentesque lacinia euismod libero. Integer ullamcorper nulla suscipit eros maximus, at sollicitudin ante faucibus. Nulla semper purus ac dignissim porttitor. Sed in nisl posuere, fermentum arcu id, ultricies nibh. Morbi a pulvinar arcu, nec varius massa. Mauris ut sagittis magna. Etiam feugiat ut neque ac congue. Maecenas quis felis eget elit placerat venenatis ut ut nisl. Sed condimentum libero condimentum bibendum imperdiet. Sed non libero a odio faucibus consectetur. Nunc dignissim nisl ut nibh lacinia, a commodo tellus venenatis. Aenean diam diam, volutpat at aliquet id, tempor ut odio. Nunc sit amet egestas lectus, in consequat magna."
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:fontFamily="sans-serif-light"
                android:layout_marginRight="16dp"
                android:layout_marginTop="13.5dp"
                android:id="@+id/textView2" />
        </LinearLayout>

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

    <Button
        android:id="@+id/toolbar_Button_Play"
        android:text="@string/play_now"
        android:layout_width="300dp"
        android:background="@color/blue"
        android:textColor="@color/white"
        app:layout_anchor="@id/appBar_layout"
        app:layout_anchorGravity="bottom|center"
        android:layout_height="wrap_content" />

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

如何实现按钮不隐藏?

更新

我注意到如果我将Button替换为android.support.design.widget.FloatingActionButton,它就会隐藏起来。所以问题在于小部件。任何人都知道如何防止这种情况发生?

1 个答案:

答案 0 :(得分:0)

这应该有用,它说因为没有人声明了ID,现在Button声明了id appBar_layout,而barLayout只使用它。我还加了海拔。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_game_info"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/toolbar_Button_Play"
        android:text="@string/play_now"
        android:layout_width="300dp"
        android:background="@color/blue"
        android:textColor="@color/white"
        app:layout_anchor="@+id/appBar_layout"
        app:layout_anchorGravity="bottom|center"
        android:elevation="10dp"
        android:layout_height="wrap_content" />

    <android.support.design.widget.AppBarLayout
        android:id="@id/appBar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@android:color/transparent"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginBottom="-70dp"
            app:expandedTitleMarginEnd="64dp"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@+id/imageDescTropos"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:scaleType="fitXY"
                android:contentDescription="Nothing"
                android:src="@drawable/download"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolBar"
                android:layout_width="match_parent"
                app:titleEnabled="false"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

        </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:background="@color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


            <TextView
                android:id="@+id/game_full_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="13.5dp"
                android:layout_marginLeft="16dp"
                android:scrollbars="vertical"
                android:text="Game Name"
                android:fadeScrollbars="true"
                android:fontFamily="sans-serif-black"
                android:textColor="@color/black"
                android:textSize="18sp"
                tools:targetApi="jelly_bean" />

            <TextView
                android:id="@+id/my_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Category"
                android:layout_marginLeft="16dp"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="9dp"
                android:background="@color/game_info_jackpot_background">

                <TextView
                    android:text="Text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif-light"
                    android:layout_marginTop="9.5dp"
                    android:layout_marginBottom="9dp"
                    android:layout_marginLeft="16dp"
                    android:id="@+id/textView" />
            </LinearLayout>

            <TextView
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer iaculis luctus elementum. Maecenas ultricies volutpat mollis. Pellentesque lacinia euismod libero. Integer ullamcorper nulla suscipit eros maximus, at sollicitudin ante faucibus. Nulla semper purus ac dignissim porttitor. Sed in nisl posuere, fermentum arcu id, ultricies nibh. Morbi a pulvinar arcu, nec varius massa. Mauris ut sagittis magna. Etiam feugiat ut neque ac congue. Maecenas quis felis eget elit placerat venenatis ut ut nisl. Sed condimentum libero condimentum bibendum imperdiet. Sed non libero a odio faucibus consectetur. Nunc dignissim nisl ut nibh lacinia, a commodo tellus venenatis. Aenean diam diam, volutpat at aliquet id, tempor ut odio. Nunc sit amet egestas lectus, in consequat magna."
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:fontFamily="sans-serif-light"
                android:layout_marginRight="16dp"
                android:layout_marginTop="13.5dp"
                android:id="@+id/textView2" />
        </LinearLayout>

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



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