【问题标题】:Android BottomSheet like Share Sheet with anchored view at bottomAndroid BottomSheet 与底部锚定视图类似的共享表
【发布时间】:2016-06-07 14:46:34
【问题描述】:

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

【问题讨论】:

    标签: android bottom-sheet


    【解决方案1】:

    您需要为 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);
    

    【讨论】:

    • 不完全是,因为我需要它是一个模态表,就像谷歌的共享表一样。模态表出现得很好,而且很慢,同时使屏幕的其余部分变暗。底部的两个按钮固定在屏幕下方,而中间的嵌套滚动视图滚动。
    【解决方案2】:

    我也遇到了同样的问题。我使用这些步骤解决了。

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

    这只是一种解决方法。基本上我所做的就是复制持久的底部表,使其表现得像模态表。

    【讨论】:

    • 谢谢 - 是的,这听起来很像黑客。我正在寻找一个干净的、面向未来的解决方案。如果不覆盖 onlayout 方法或其他方法,似乎没有办法做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2012-10-04
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2014-05-29
    相关资源
    最近更新 更多