首页 > 解决方案 > 在工具栏上方显示粘滞按钮

问题描述

我有以下布局:

<LinearLayout
        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:orientation="vertical"
        android:layout_width="match_parent" android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/Toolbar"/>

    <com.sample.android.scrolltricks.ObservableScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

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

                <ImageView style="@style/Item"
                           android:scaleType="centerCrop"
                           android:src="@drawable/london_flat"
                           tools:ignore="contentDescription"/>

                <View android:id="@+id/placeholder"
                      android:layout_width="match_parent"
                      android:layout_height="@dimen/sticky_height"/>

                <View style="@style/Item.Bottom"/>
                <View style="@style/Item.Bottom.Alt"/>
                <View style="@style/Item.Bottom"/>
                <View style="@style/Item.Bottom.Alt"/>
                <View style="@style/Item.Bottom"/>
                <View style="@style/Item.Bottom.Alt"/>

            </LinearLayout>

            <Button android:id="@+id/sticky" style="@style/Sticky"/>

        </FrameLayout>

    </com.sample.android.scrolltricks.ObservableScrollView>

</LinearLayout>

这是滚动时显示粘滞按钮的代码:

override fun onScrollChanged(scrollY: Int) {
        sticky.translationY = Math.max(
            placeholder.top - resources.getDimension(R.dimen.sticky_height) / 2,
            scrollY.toFloat() - 65
        )
    }

粘性按钮显示在工具栏下方,我想将其部分显示在工具栏上方。( scrollY.toFloat() - 65)

你知道怎么解决吗?

标签: androidandroid-scrollview

解决方案


不幸的是,在您的布局中,您无法将其Button置于ScrollView. 你Button是 的孩子FrameLayout,它是它ScrollView自己的孩子,因此它的绘图区域受到父母的界限的限制。

最好通过包装所有内容来解决此类任务,CoordinatorLayout然后CoordinatorLayout.BehaviorView您想要具有特定定位的对象上应用自定义。

希望能帮助到你。


推荐阅读