简体   繁体   English

DialogFragment占据整个屏幕宽度

[英]DialogFragment takes whole screen width

in my android app I'm using DialogFragment extending class to show custom dialogs, but I have a problem all created dialogs take whole screen width (example in photo), I would prefer it come with margin, how to do it? 在我的Android应用程序中,我正在使用DialogFragment扩展类来显示自定义对话框,但是我有一个问题,所有创建的对话框都占据整个屏幕宽度(照片中的示例),我希望它带有边距,该怎么做?

my dialog 我的对话

one way is to use style for it. 一种方法是使用样式。 You could create your custom style, like 您可以创建自定义样式,例如

<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> 

and onCreate of your DialogFragment subclass you can provide it as parameter DialogFragment子类的onCreate ,您可以将其作为参数提供

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

you can find a description of the key items in the style here 您可以在此处找到样式中关键项目的描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM