在我的Android应用程序中我使用DialogFragment扩展类来显示自定义对话框,但我有一个问题所有创建的对话框采取整个屏幕宽度(照片中的示例),我更喜欢它带有保证金,如何做到这一点?
一种方法是使用它的风格。您可以创建自定义样式,例如
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">@color/black</item>
<item name="android:textColorPrimary">@color/black</item>
<item name="android:windowMinWidthMajor">80%</item>
<item name="android:windowMinWidthMinor">80%</item>
<!-- Used for the background -->
<item name="android:windowBackground">@color/white</item>
</style>
和onCreate
子类的DialogFragment
你可以提供它作为参数
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.AlertDialogStyle)
你可以找到风格here中的关键项目的描述