如何使 BottomSheetDialog 匹配父高度(全屏)
Posted
技术标签:
【中文标题】如何使 BottomSheetDialog 匹配父高度(全屏)【英文标题】:How can I make BottomSheetDialog match parent height (full screen) 【发布时间】:2020-01-06 05:13:13 【问题描述】:这是我使用的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:layout_gravity="top|center_horizontal"
android:background="@color/white"
android:gravity="top|center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/provisioningTitle"
android:layout_
android:layout_
android:text="Phone Number" />
</LinearLayout>
这是我创建对话框的类:
open class DialogProvisioningData : BottomSheetDialog
constructor(context: Context) : super(context)
private lateinit var mBehavior: BottomSheetBehavior<FrameLayout>
override fun setContentView(view: View)
super.setContentView(view)
val bottomSheet = window.decorView.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout
mBehavior = BottomSheetBehavior.from(bottomSheet)
mBehavior.peekHeight = Resources.getSystem().getDisplayMetrics().heightPixels
mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
override fun onStart()
super.onStart()
mBehavior.peekHeight = Resources.getSystem().getDisplayMetrics().heightPixels
mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
companion object
fun newInstance(context: Context): DialogProvisioningData
val dialog = DialogProvisioningData(context)
var layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater;
val bottomSheet = layoutInflater.inflate(R.layout.dialog_provisioning, null)
bottomSheet.layout.setOnClickListener( dialog.cancel() )
dialog.setOnShowListener dialog ->
val d = dialog as BottomSheetDialog
val bottomSheet = d.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
BottomSheetBehavior.from(bottomSheet!!).state = BottomSheetBehavior.STATE_EXPANDED
dialog.setContentView(bottomSheet)
dialog.show()
return dialog
要让 BottomSheetDialog 真正全屏显示,我需要进行哪些更改?我已将状态设置为展开,并将 peekHeight 设置为屏幕高度
【问题讨论】:
这能回答你的问题吗? Set BottomSheetDialogFragment height to full screen 【参考方案1】:不要使用全屏 BottomSheet。 您可以从 DialogFragment 使用
用于显示对话框:
FragmentManager fragmentManager = getSupportFragmentManager();
DialogFullscreenFragment newFragment = new DialogFullscreenFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, newFragment).addToBackStack(null).commit();
DialogFullscreenFragment 类:
public class DialogFullscreenFragment extends DialogFragment
private View root_view;
private Slider slider;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
root_view = inflater.inflate(R.layout.dialog_event, container, false);
return root_view;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
【讨论】:
我不想要 Fragment,我可以使用 CustomDialog 而不是 BottomSheetView。但我不想在我的活动中有另一个片段。所以我宁愿不使用 DialogFragment【参考方案2】:我们可以通过像这样设置我们的view.minHeight
来解决它:
class SortProductBottomSheet : BaseBottomSheet()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.dialog_blablabla, container, false)-
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
//THIS IS OUR LIFESAVER
val dm = Resources.getSystem().displayMetrics
val rect = dm.run Rect(0, 0, heightPixels, widthPixels)
view.minimumHeight = rect.height()
【讨论】:
【参考方案3】:在尝试了多种实现之后,这是我最满意的。它是全屏的,它是一个对话框,并且它还具有我需要的正确 SlideUpAnimation。此外,从片段内部调用 if 很容易,而无需关闭我自己的片段:
open class DialogProvisioningData : Dialog
constructor(context: FragmentActivity, themeResId: Int) : super(context, themeResId)
companion object
val TAG: String = "DialogProvisioningData"
fun newInstance(context: FragmentActivity): DialogProvisioningData
val dialog = DialogProvisioningData(context, R.style.DialogAnimation)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation
var layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val bottomSheet = layoutInflater.inflate(R.layout.dialog_provisioning, null)
dialog.setContentView(bottomSheet)
dialog.show()
return dialog
【讨论】:
以上是关于如何使 BottomSheetDialog 匹配父高度(全屏)的主要内容,如果未能解决你的问题,请参考以下文章
如何在BottomSheetDialog android的顶部中心添加关闭按钮?
如何制作只有上角圆角的BottomSheetDialog? [复制]
BottomSheetDialog 内 VideoView 的 MediaController 隐藏在 BottomSheetDialog 后面