我有以下布局:
<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
)
你知道怎么解决吗?
不幸的是,在你的布局中,你无法将Button
带到ScrollView
之上。你的Button
是FrameLayout
的孩子,它是ScrollView
本身的孩子,因此它的绘图区域受到父母界限的限制。
最好通过在CoordinatorLayout
中包装所有内容来解决此类任务,然后在CoordinatorLayout.Behavior
上应用自定义View
,以获得特定的定位。
希望能帮助到你。