首先,我将向您展示 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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<androidx.core.widget.NestedScrollView
android:id="@+id/nested"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:overScrollMode="never">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="56dp"
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_width="match_parent"
android:layout_height="56dp"
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_width="match_parent"
android:layout_height="56dp"
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_width="match_parent"
android:layout_height="56dp"
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_width="match_parent"
android:layout_height="56dp"
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_width="match_parent"
android:layout_height="56dp"
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_width="match_parent"
android:layout_height="91dp"
android:visibility="gone"
tools:visibility="visible">
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="36dp"
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_width="28dp"
android:layout_height="28dp"
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>
first
实现了 Android 键盘,以便按下 Button id到的任何按钮sixth
。但是这里的问题是,当键盘向上时,视图焦点设置在EditText上,但是EditText被键盘遮挡了。有没有办法让 BottomSheetDialogFragment 像 Activity 一样调整视图的大小?
[更新]
作为参考,样式应用如下。
- 调整大小
- 状态始终可见
- 调整大小,状态始终可见
我尝试了所有三种情况,但它没有奏效。
<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>