在我看来,我已经阅读了关于stackoverflow的所有主题,但尚未找到解决方案。
问题是ImageView始终位于RecyclerView下,但是我需要在RecyclerView顶部显示ImageView。
问题:为什么会这样,我该如何解决?
对与错示例:
我的XML:
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bottom_style"
android:overScrollMode="never"
app:behavior_hideable="false"
app:behavior_peekHeight="150dp"
app:layout_behavior="....BottomSheetController.MyBottomSheetBehavior">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<....BottomSheetController.MyRecyclerView
android:id="@+id/recycler_view"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:overScrollMode="never" />
<ImageView
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_button_close" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.core.widget.NestedScrollView>
尝试下面的代码
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorAccent"
android:overScrollMode="never"
app:behavior_hideable="false"
app:behavior_peekHeight="150dp"
app:layout_behavior="....BottomSheetController.MyBottomSheetBehavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<....BottomSheetController.MyRecyclerView
android:id="@+id/recycler_view"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:overScrollMode="never" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<ImageView
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_button_close" />
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
如果不使用RelativeLayout
,是否有任何特殊原因需要将内容包装在FrameLayout
中,这可以解决您的问题,请记住将android:layout_gravity="end|top"
设置为将关闭按钮放在顶端。因此,代码如下所示:
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bottom_style"
android:overScrollMode="never"
app:behavior_hideable="false"
app:behavior_peekHeight="150dp"
app:layout_behavior="....BottomSheetController.MyBottomSheetBehavior">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<....BottomSheetController.MyRecyclerView
android:id="@+id/recycler_view"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:overScrollMode="never" />
<ImageView
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|top"
android:src="@drawable/ic_button_close" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.core.widget.NestedScrollView>
作为原因,据我所知,为什么RelativeLayout显示不正确的输出是因为z轴上的视图距离是根据生成它们的顺序设置的。因此,即使ImageView是在recyclerview之后设置的,也位于顶部,但是里面的支架是在之后生成的,因此位于imageview的顶部。如果它是在imageview之前生成的简单视图,则图像将根据需要位于顶部。
P.S .:我没有找到任何与此相关的文档,而我纯粹是相信它的工作原理,因此,如果不是这样,那么任何人都可以自由地纠正我。