Android BottomSheet 类似于 Share Sheet,底部有锚定 View

标签 android bottom-sheet

我正在尝试使用支持库的 BottomSheetDialogFragment 来复制在您点击共享按钮时显示的标准工作表(见下文)。我将如何实现类似的布局,顶部有一个标题,中心有独立可滚动的内容,但底部锚定 View 的按钮始终位于顶部。

enter image description here

最佳答案

您需要为 bottomSheet 构建自定义布局,例如 share_bottom.xml。在那个布局中你可以

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/bottom_sheet_behavior"
    app:behavior_hideable="true"
    app:behavior_peekHeight="200dp">
  <TextView
        android:layout_width="match_parent"
        android:text="header"
        android:layout_height="wrap_content"/>
   <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_weight="0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

然后将其包含在 fragment 布局的底部:

之后您可以控制此工作表的可见性

//retrieve the bottomsheet
bottomSheet = (LinearLayout) findViewById(R.id.bottomSheet);
//get the behaviour controller
bsb = BottomSheetBehavior.from(bottomSheet);
//hide the sheet
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
//showthe sheet
bsb.setState(BottomSheetBehavior.STATE_EXPANDED);

关于Android BottomSheet 类似于 Share Sheet,底部有锚定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37682530/

相关文章:

java - JAVA 编译器是否处理函数作用域内的新实例化?

java - locationManager.getLastKnownLocation 请求后返回 null

android - 如何在不使用对话框的情况下在显示 Bottom Sheet 时使背景变暗?

java - 如何在android中释放对象?

java - 我如何为每个单独的 recyclerView 设置数据?

android - 无法在 eclipse 中运行 traceview 以进行 android 分析

android - 像 super 应用程序一样的 Bottom Sheet

android - Bottomsheet 总是隐藏我最后的 Recyclerview 元素

ios - 在 UISheetPresentationController 中自定义较小的 Detents?

带有 RecyclerView 的 Android BottomSheet