gpt4 book ai didi

android - 如何保持BottomSheetDialogFragment的高度始终为match_parent?

转载 作者:行者123 更新时间:2023-12-03 10:12:18 25 4
gpt4 key购买 nike

我几天前在一个项目中实现的BottomSheetDialogFragment遇到了麻烦。
发生的是,我有一个BottomSheet,其中包含SearchViewRecyclerview。对话框片段正确显示了东西,一切都很好。
当我使用SearchView过滤Recyclerview的结果时,问题就开始了,因为当结果不超过5个时,键盘与现在很小的Recyclerview重叠。
我想知道是否可以将BottomSheet的高度保持为match_parent或某种东西来填充窗口,或者将Recyclerview保持足够大以避免键盘“弄乱”结果。我使用以下方法使片段在打开时扩展:

private fun expandBottomSheet() {
view?.viewTreeObserver?.addOnGlobalLayoutListener {
val dialog = dialog as BottomSheetDialog

val bottomSheet = dialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)

val behavior = BottomSheetBehavior.from<View>(bottomSheet)

behavior.state = BottomSheetBehavior.STATE_EXPANDED
}

}

我在工作表中的XML是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/bottom_sheet_behavior">

<View
android:layout_width="52dp"
android:layout_height="7dp"
android:layout_gravity="center"
android:layout_marginTop="@dimen/size_small_4"
android:layout_marginBottom="@dimen/size_small_4"
android:background="@drawable/border_top_swipe_indicator" />

<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:iconifiedByDefault="false"
app:queryHint="@string/text_type_your_query" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border_top_white"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/list_item" />

</LinearLayout>

在此先感谢您的帮助!

编辑:
包含 Recyclerview和东西的 Bottom Sheet 是一个子片段(从另一个片段实例化的片段)。

最佳答案

在manifest.xml文件中,您已为应用程序声明了 Activity 。在承载此 Bottom Sheet 的<activity>块内部,您可以声明窗口软输入模式,以使键盘不会与 View 重叠-而是将其向上推。

<activity
...
android:windowSoftInputMode="adjustResize|stateVisible"> ... </activity>

stateVisible:“通常情况下(当用户向前导航到 Activity 的主窗口时),软键盘才可见。”

AdjustResize:“ Activity 的主窗口始终会调整大小,以便为屏幕上的软键盘腾出空间。”

Docs

那应该为您工作。根据您的观点,可能会导致UI和UX较差。如果是这样,您可以在搜索 View 上设置焦点监听器,并在获得焦点时以编程方式将 Bottom Sheet 的状态设置为扩展。参见答案 here

关于android - 如何保持BottomSheetDialogFragment的高度始终为match_parent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57528390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com