Android 底部对话框,类似于分享对话框,并在底部固定视图。

3
我正在尝试使用支持库的BottomSheetDialogFragment来复制标准底部菜单,当您点击分享按钮时会显示该菜单(如下所示)。我该如何实现类似的布局,在顶部有一个标题,在中心有独立可滚动的内容,但是底部固定了一些按钮,始终保持在顶部。

enter image description here

2个回答

0

我之前也遇到了同样的问题,但是我通过以下步骤解决了它:

  • 将FrameLayout设置为根布局。
  • 将需要锚定的视图作为第二个子项包含在其中,第一个子项应包含活动的所有内容。
  • 当底部表单展开时,使第二个子项可见,并在底部表单折叠时将其隐藏。
  • 可以通过在底部表单中使用滚动视图或嵌套滚动视图来实现独立滚动的内容。
  • 要调暗背景,请参考this link

这只是一个解决方法。基本上,我复制了持久底部表单以像模态表单一样运行。


1
谢谢 - 是的,那听起来很像黑客技巧。我正在寻找一个干净、未来可靠的解决方案。似乎没有办法在不覆盖onlayout方法或类似方法的情况下实现这个。 - strangetimes

0

你需要为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>

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

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

//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);

不太对,因为我需要它像谷歌的共享表单一样成为模态表单。模态表单会很好地弹出,并缓慢地将屏幕其余部分变暗。底部的两个按钮会固定在屏幕下方,而中心的嵌套滚动视图会滚动。 - strangetimes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接