android - 我如何为我的 BottomSheet 设置半展开状态
问题描述
我有底部表的布局。
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="@color/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main_weather_map" />
<include layout="@layout/bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
底页布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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="match_parent"
android:background="@android:color/white"
android:clipToPadding="true"
app:behavior_peekHeight="80dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/weather_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
tools:listitem="@layout/item_weather" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
对我来说,我的底页必须打开前半部分,然后重新拖动它以全屏显示。它是如何在谷歌地图应用程序中完成的。但我不知道该怎么做。
解决方案
最好使用具有全部潜力的框架。正如方法setFitToContents的官方文档所述:
设置展开的工作表的高度是由其内容的高度决定,还是分两个阶段展开(父容器的一半高度,父容器的全高)。默认值为真。
所以你需要的setFitToContent
只是false
:
behavior = BottomSheetBehavior.from(your_bottom_sheet_xml)
behavior.isFitToContents = false
behavior.halfExpandedRatio = 0.6f
使用这个 3 行代码,底部表格将首先扩展至屏幕的 60%,然后将完全扩展至 100%。
希望能帮助到你!
推荐阅读
- docker - 在 docker-compose 文件中指定端口是否会覆盖 Dockerfile 中公开的端口?
- visual-studio-code - Visual Studio 代码空间错误
- javascript - Selenium C# - 如何从锚标记中删除或替换 target=_blank
- python - 为什么我在 Python 控制台输入 2 或 3 时没有得到 True?
- javascript - 如何使用 webdriver 确定元素是否被父样式隐藏
- npm - npm 错误的问题
- html - 渲染 ejs 文件时样式表和图像未反映
- python-2.7 - 运行 flopy.modflow.HeadObservation 时出错:ValueError:不能从结构转换为非结构,除非结构只有一个字段
- excel - 如何根据同一行中的其他值对同一列中的值求和
- c# - 立即更改代理 C#