将布局锚定到底部 sheet
Anchoring layout to bottom sheet
我正在尝试将视图锚定到底部的顶部和水平中心 sheet 但我 运行 遇到了问题。当我将附加到底部的视图的锚引力 sheet 设置为 center_horizontal
时,视图会稍微偏离中心。
当前XML代码:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pull_tab"
android:orientation="vertical"
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="center_horizontal"
android:layout_gravity="top"
>
</LinearLayout>
<RelativeLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="@android:color/holo_blue_bright"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_draggable="true"
app:behavior_hideable="true"
app:behavior_peekHeight="55dp">
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Result
编辑: 我在这里想要完成的是让锚定视图始终在底部的水平中心可见 sheet 即使底部 sheet 已隐藏。
按照下面的代码
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
bottom_sheet.xml
<RelativeLayout 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:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="100dp"
android:layout_height="15dp"
android:background="@color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/grey1" />
</LinearLayout>
</RelativeLayout>
我正在尝试将视图锚定到底部的顶部和水平中心 sheet 但我 运行 遇到了问题。当我将附加到底部的视图的锚引力 sheet 设置为 center_horizontal
时,视图会稍微偏离中心。
当前XML代码:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pull_tab"
android:orientation="vertical"
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="center_horizontal"
android:layout_gravity="top"
>
</LinearLayout>
<RelativeLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="@android:color/holo_blue_bright"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_draggable="true"
app:behavior_hideable="true"
app:behavior_peekHeight="55dp">
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Result
编辑: 我在这里想要完成的是让锚定视图始终在底部的水平中心可见 sheet 即使底部 sheet 已隐藏。
按照下面的代码
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
bottom_sheet.xml
<RelativeLayout 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:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="100dp"
android:layout_height="15dp"
android:background="@color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/grey1" />
</LinearLayout>
</RelativeLayout>