首页 > 解决方案 > 状态展开的底片圆角

问题描述

我面临一个特定的问题。我有一个 bottomSheet,我想在 TopLeft 和 TopRight 角上放置一个半径。除非 bottomSheet.state 为STATE_EXPANDED. 所以我找到了这个解决方案

@SuppressLint("RestrictedApi", "VisibleForTests")
override fun onCreateDialog(savedInstanceState: Bundle?): BottomSheetDialog {
    val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
    bottomSheetDialog.behavior.disableShapeAnimations()
    return bottomSheetDialog
}

但问题是:在我的bottomSheet 中,我有一个EditText 和一个按钮。使键盘在按钮下方,我必须在我的onCreateDialog 方法中添加这行代码

bottomSheetDialog.behavior.state = STATE_EXPANDED

所以当我添加它时,由于某种原因,角落不再是圆的......

在这里你可以找到我当前的代码:

@SuppressLint("RestrictedApi", "VisibleForTests")
override fun onCreateDialog(savedInstanceState: Bundle?): BottomSheetDialog {
    val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
    bottomSheetDialog.behavior.disableShapeAnimations()
    bottomSheetDialog.behavior.state = STATE_EXPANDED
    return bottomSheetDialog
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetStyle)
}

在这里我的Style

<style name="BottomSheetStyle" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
</style>

你知道我能做些什么来完成这项工作吗?bottomSheetDialog.behavior.disableShapeAnimations()

标签: androidkotlinandroid-bottomsheetdialog

解决方案


尝试使用以下代码。

//create round_dialog.xml inside drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners
    android:bottomLeftRadius="24dp"
    android:bottomRightRadius="24dp"
    android:topLeftRadius="24dp"
    android:topRightRadius="24dp" />
</shape>


// update you theme file
    <style name="AddDocBottomSheetDialogTheme" 
parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>

<style name="AppModalStyle" 
parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/rounded_dialog</item>
<item name="android:layout_marginHorizontal">15dp</item>
</style>



// Dialog class
class TestDialogFragment : BottomSheetDialogFragment() {
private lateinit var binding: TestDialogFragmentBinding

override fun getTheme(): Int {
    return R.style.AddDocBottomSheetDialogTheme
}
}

推荐阅读