显示工具栏上方的粘滞按钮

问题描述 投票:1回答:2

我有以下布局:

<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

你知道怎么解决吗?

android android-scrollview
2个回答
1
投票

不幸的是,在你的布局中,你无法将Button带到ScrollView之上。你的ButtonFrameLayout的孩子,它是ScrollView本身的孩子,因此它的绘图区域受到父母界限的限制。

最好通过在CoordinatorLayout中包装所有内容来解决此类任务,然后在CoordinatorLayout.Behavior上应用自定义View,以获得特定的定位。

希望能帮助到你。


© www.soinside.com 2019 - 2024. All rights reserved.