Android 在 XML 中设置 Bottom Sheet 单状态

标签 android android-layout bottom-sheet

XML 中的 Bottom Sheet 有几种状态:STATE_COLLAPSED、STATE_EXPANDED、STATE_HIDDEN 等。

如何使用可观察对象在 XML 中设置集合?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="@string/bottom_sheet_behavior"
    <!-- Example of State -->
    app:bottom_state="@{viewModel.state}"
    >
</LinearLayout>

显然,底部状态的名称不正确,但我正在努力弄清楚它是什么。

最佳答案

没有直接的 xml 值可用于 Bottom Sheet 状态。但是如果你想在数据绑定(bind)中做到这一点,还有另一种选择。为此创建一个绑定(bind)适配器并使用它。

@BindingAdapter("bottomSheetState")
fun bindingBottomSheet(container: LinearLayout, state:Int) {
    val behavior=BottomSheetBehavior.from(container)
    behavior.state = state
}

现在在xml中你可以使用它了

<LinearLayout
    android:layout_height="340dp"
    android:layout_width="match_parent"
    app:behavior_hideable="true"
    app:behavior_peekHeight="80dp"
    app:bottomSheetState="@{model.uiState.bottomSheetState}"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

关于Android 在 XML 中设置 Bottom Sheet 单状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55682256/

相关文章:

android - 我真的需要多少布局资源文件夹来覆盖所有 Android 设备

android - Bottom Sheet 向下滑动行为

android - Flutter: `Container` 高度无法在 `Stack` 内部调整

android - 使用 Qt Creator 为 Android 构建 Qt 应用程序时出错

android - 如何在Android中定义 "inflation"

android - 如何将整个屏幕的 fragment 最小化到屏幕底部

带有 RecyclerView 的 Android ViewPager 在 BottomSheet 中工作不正确

android - 谷歌地图触摸 Bottom Sheet 对话框

android - 问题添加 Android 对话框

java - 提取加载完整图像的路径 Recyclerview Cardview