DialogFragment占据整个屏幕宽度

问题描述 投票:1回答:1

在我的Android应用程序中我使用DialogFragment扩展类来显示自定义对话框,但我有一个问题所有创建的对话框采取整个屏幕宽度(照片中的示例),我更喜欢它带有保证金,如何做到这一点?

my dialog

android dialog width
1个回答
1
投票

一种方法是使用它的风格。您可以创建自定义样式,例如

<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中的关键项目的描述

© www.soinside.com 2019 - 2024. All rights reserved.