如何使 MaterialAlertDialogBuilder 居中对齐标题、消息和按钮
how to make center MaterialAlertDialogBuilder alignment title, message & button
如何使标题、消息和按钮居中对齐
MaterialAlertDialogBuilder(requireContext())
.setTitle("Message")
.setMessage("This is info message.")
.setPositiveButton("Ok") { dialog, which ->
}
.show()
您可以创建自己的布局并将其添加到对话框中
val inflater=LayoutInflater.from(requireContext())
//Getting the custom view
val view=inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT,null)
//Obtaining the components
val title=view.findViewById(R.id.YOUR_CUSTOM_TILE)
title.text="Message"
val button=view.findViewById(R.id.YOUR_CUSTOM_BUTTON)
button.setOnCLickListener{
//onClick
}
val dialog=MaterialAlertDialogBuilder(requireContext()).setView(view).create()
dialog.show()
对于标题和消息,您可以使用:
<style name="MaterialAlertDialog__Center" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialAlertDialogTitlePanelStyle">@style/Title.Panel.Center</item>
<item name="materialAlertDialogBodyTextStyle">@style/Message.Center</item>
</style>
<style name="Title.Panel.Center" parent="MaterialAlertDialog.MaterialComponents.Title.Panel">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="Message.Center" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:gravity">center_horizontal</item>
</style>
与:
MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialog_Center)
.setTitle("Message")
..
.show()
要使标题和消息居中,您只需在主题中进行更改
<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Centered</item>
如何使标题、消息和按钮居中对齐
MaterialAlertDialogBuilder(requireContext())
.setTitle("Message")
.setMessage("This is info message.")
.setPositiveButton("Ok") { dialog, which ->
}
.show()
您可以创建自己的布局并将其添加到对话框中
val inflater=LayoutInflater.from(requireContext())
//Getting the custom view
val view=inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT,null)
//Obtaining the components
val title=view.findViewById(R.id.YOUR_CUSTOM_TILE)
title.text="Message"
val button=view.findViewById(R.id.YOUR_CUSTOM_BUTTON)
button.setOnCLickListener{
//onClick
}
val dialog=MaterialAlertDialogBuilder(requireContext()).setView(view).create()
dialog.show()
对于标题和消息,您可以使用:
<style name="MaterialAlertDialog__Center" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialAlertDialogTitlePanelStyle">@style/Title.Panel.Center</item>
<item name="materialAlertDialogBodyTextStyle">@style/Message.Center</item>
</style>
<style name="Title.Panel.Center" parent="MaterialAlertDialog.MaterialComponents.Title.Panel">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="Message.Center" parent="MaterialAlertDialog.MaterialComponents.Body.Text">
<item name="android:gravity">center_horizontal</item>
</style>
与:
MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialog_Center)
.setTitle("Message")
..
.show()
要使标题和消息居中,您只需在主题中进行更改
<item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Centered</item>