【问题标题】:DialogFragment takes whole screen widthDialogFragment 占用整个屏幕宽度
【发布时间】:2019-08-06 12:30:22
【问题描述】:

在我的 android 应用程序中,我正在使用 DialogFragment 扩展类来显示自定义对话框,但我有一个问题,所有创建的对话框都占用整个屏幕宽度(照片中的示例),我希望它带有边距,怎么做?

my dialog

【问题讨论】:

  • 分享您尝试过的代码,以便我们看到您错过了什么
  • 抱歉,但在尝试实施任何解决方案后,我发现我正在删除它,让我的对话框的类为空(只是类对话框:FragmentDialog(){}),所以我认为没有什么可做的在这里显示

标签: android dialog width


【解决方案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> 

DialogFragment 子类的onCreate 可以将其作为参数提供

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

您可以在here样式中找到关键项目的描述

【讨论】:

  • 非常感谢,这么简单优雅的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 2017-01-24
  • 2021-04-05
  • 2019-08-05
相关资源
最近更新 更多