如何在 BottomSheetDialogFragment 中应用屏幕调整大小?
Posted
技术标签:
【中文标题】如何在 BottomSheetDialogFragment 中应用屏幕调整大小?【英文标题】:How to apply screen resize in BottomSheetDialogFragment? 【发布时间】:2022-01-22 03:01:34 【问题描述】:首先,我将向您展示 BottomSheetDialogFragment 类。
class BottomSheetReportFragment(var mContext: Context, var postId: String, var commentId: String?, var replyId: String?) : BottomSheetDialogFragment()
private lateinit var mView: View
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppBottomSheetDialogTheme)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
mView = inflater.inflate(R.layout.bottom_sheet_report, container, false)
return mView
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener dialogInterface ->
val bottomSheetDialog = dialogInterface as BottomSheetDialog
setupRatio(bottomSheetDialog)
return dialog
private fun setupRatio(bottomSheetDialog: BottomSheetDialog)
val bottomSheet = bottomSheetDialog.findViewById<FrameLayout>(R.id.design_bottom_sheet)
val behavior = BottomSheetBehavior.from(bottomSheet!!)
val layoutParam = bottomSheet.layoutParams as ViewGroup.LayoutParams
layoutParam.height = getBottomSheetDialogDefaultHeight()
bottomSheet.layoutParams = layoutParam
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
behavior.peekHeight = getBottomSheetDialogDefaultHeight()
private fun getBottomSheetDialogDefaultHeight(): Int
return getWindowHeight() * 85 / 100
private fun getWindowHeight(): Int
val displayMetrics = mContext.resources.displayMetrics
return displayMetrics.heightPixels
xml是这样的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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_
android:layout_
android:orientation="vertical"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<androidx.core.widget.NestedScrollView
android:id="@+id/nested"
android:layout_
android:layout_
android:layout_weight="1"
android:overScrollMode="never">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_
android:layout_
android:orientation="vertical">
<Button
android:id="@+id/first"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:background="@drawable/selector_button_report"
android:button="@null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="1"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="@+id/second"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:background="@drawable/selector_button_report"
android:button="@null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="2"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="@+id/third"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:background="@drawable/selector_button_report"
android:button="@null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="3"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="@+id/fourth"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:background="@drawable/selector_button_report"
android:button="@null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="4"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="@+id/fifth"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:background="@drawable/selector_button_report"
android:button="@null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="5"
android:textColor="#1C1C1C"
android:textSize="16dp" />
<Button
android:id="@+id/sixth"
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:background="@drawable/selector_button_report"
android:button="@null"
android:paddingStart="14dp"
android:paddingEnd="14dp"
android:text="6"
android:textColor="#1C1C1C"
android:textSize="16dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
<RelativeLayout
android:id="@+id/relative"
android:layout_
android:layout_
android:visibility="gone"
tools:visibility="visible">
<EditText
android:id="@+id/input"
android:layout_
android:layout_
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="@drawable/shape_edittext_comment"
android:includeFontPadding="false"
android:inputType="textMultiLine"
android:lines="2"
android:paddingStart="16dp"
android:paddingEnd="73dp"
android:textColor="#000000"
android:textColorHint="#ADB1BA"
android:textSize="14dp" />
<ImageView
android:id="@+id/send"
android:layout_
android:layout_
android:layout_alignTop="@id/input"
android:layout_alignBottom="@id/input"
android:layout_alignParentEnd="true"
android:layout_marginTop="4dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="4dp"
android:backgroundTint="#CBCBCB"
android:src="@android:drawable/ic_menu_send" />
</RelativeLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
Android 键盘的实现使得从 Button id first
到 sixth
的任何按钮都被按下。但是这里的问题是,当键盘向上时,视图焦点设置在EditText上,而EditText却被键盘挡住了。有没有办法让 BottomSheetDialogFragment 像 Activity 一样调整视图的大小?
[更新]
作为参考,样式应用如下。
-
调整大小
stateAlwaysVisible
adjustResize、stateAlwaysVisible
我尝试了所有三种情况,但都没有成功。
<style name="AppBottomSheetDialogTheme" parent="Theme.Design.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize|stateAlwaysVisible</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/shape_bottom_sheet</item>
</style>
【问题讨论】:
【参考方案1】:在您的AppBottomSheetDialogTheme
中,添加android:windowSoftInputMode
。
<style name="AppBottomSheetDialogTheme">
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
【讨论】:
感谢您的回复。编辑了问题。感谢您的收看。以上是关于如何在 BottomSheetDialogFragment 中应用屏幕调整大小?的主要内容,如果未能解决你的问题,请参考以下文章
如何在异步任务中调用意图?或者如何在 onPostExecute 中开始新的活动?