代码之家  ›  专栏  ›  技术社区  ›  Grzegorz Adam Hankiewicz

如何使AlertDialog显示在屏幕边缘附近

  •  0
  • Grzegorz Adam Hankiewicz  · 技术社区  · 6 年前

    我有一个 alertdialog Working fine on most android devices.该对话框将从底部显示其布局,并绘制在屏幕底部边缘附近。但是,在小米米8设备上,对话框悬停在底部边缘上方。这个设备的屏幕角是圆角的,我认为对话框试图 避开它们。然而,这在美学上看起来非常糟糕。如何创建对话框,使其显示在 all devices中屏幕边缘附近,而不管它们的屏幕角是什么?

    不幸的是,stackoverflow充满了关于使用圆角进行对话的问题,我找不到任何有用的地方。最近的是 这个关于状态栏的问题 但是那里的建议没有为 alertdialog更改对话行为 on the mi8.

    为了展示我提出的问题, a minimal example project at commit 763bc62ff9320227b806260e861b63d48c2347d The following screenshot shows the problem on the mi8,where the full screen mode leaves the dialog floating above the edge of the screen,and it seek likee它在顶部的覆盖层也做了一些奇怪的事情。

    由于警报对话框声明为浮动,因此小米MI8在屏幕角上方的“警报”对话框处于“推送”状态。将样式更改为 windowsisfloating=false后,对话框将显示在屏幕边缘附近:

    <style name=“dialog”parent=“theme.appcompat.dialog.alert”>
    <item name=“android:windowbackground”>@android:color/transparent</item>
    <item name=“android:background”>@android:color/transparent</item>
    <item name=“android:windowisfloating”>错误</item>
    <item name=“android:backgrounddimenabled”>错误</item>
    <item name=“android:windowframe”>@null</item>
    <item name=“android:windowcontentoverlay”>@null</item>
    <item name=“android:windowanimationstyle”>@style/mapdialoganimation</item>
    <item name=“android:windowfullscreen”>错误</item>
    <item name=“Android:WindowIsTranslucent”>正确</item>
    <item name=“android:windownotitle”>正确</item>
    <item name=“android:windowminwidthmajor”>100%</item>
    <item name=“android:windowminwidthminor”>100%</item>
    <item name=“android:colorbackgroundcachehint”>@null</item>
    
    &!--https://stackoverflow.com/a/28341624/172690-->
    <item name=“android:fitsSystemWindows”>正确</item>
    &风格/风格;
    

    不幸的是,这个解决方案适用于小米,但不适用于另一个三星SM-J530F,它在动画中显示了一个黑条:

    为了避免动画中出现黑条,我找到的唯一方法是使windowfullscreen=true。但是,当此值设置为true时,小米会制作一个奇怪的状态栏动画:

    有没有任何方法可以让这个简单的动画在所有Android设备上保持一致,而不必诉诸于几个硬编码的条件?

    update:正如建议的那样,我在commit 403808dcfb5d3fae52c51eb1593b79065ada7中更改了对话框,而不是设置动画,并通过constraint layou移动了按钮本人T集:

    .避免他们。然而,这在美学上看起来非常糟糕。如何创建对话框,使其显示在屏幕边缘附近全部的设备,不管它们的屏幕角是什么?

    不幸的是,stackoverflow充满了关于使用圆角进行对话的问题,我找不到任何有用的地方。最近的是this question regarding the status bar但是那里的建议并没有改变对话框在MI8上。

    展示我的问题a minimal example project.At commit 763bc62ff9320227b806260e861b63d48cc2347d以下屏幕截图显示了MI8上的问题,即全屏模式离开对话框浮动在屏幕边缘的上方,看起来在顶部的覆盖层也做了一些奇怪的事情。

    floating alertdialog on full screen vs normal display

    小米MI8是屏幕角上方的AlertDialog,因为AlertDialog声明为浮动。将样式更改为windowIsFloating = false对话框将显示在屏幕边缘附近:

    <style name="Dialog" parent="Theme.AppCompat.Dialog.Alert">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@style/MapDialogAnimation</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowMinWidthMajor">100%</item>
        <item name="android:windowMinWidthMinor">100%</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    
        <!-- https://stackoverflow.com/a/28341624/172690 -->
        <item name="android:fitsSystemWindows">true</item>
    </style>
    

    不幸的是,这个解决方案适用于小米,但不适用于另一个三星SM-J530F,它在动画期间显示一个黑色条:

    Samsung SM-J530F black bar animation

    为了避免动画中出现黑条,我找到的唯一方法是windowFullScreen = true.然而,当该值设置为true时,小米会制作一个奇怪的状态栏动画:

    enter image description here

    有没有任何方法可以让这个简单的动画在所有Android设备上保持一致,而不必诉诸于几个硬编码的条件?

    更新:如建议所示,而不是设置我在中更改的对话框的动画。commit 403808dcfb5d3fae52c51eb1b21593b79065ada7不通过约束布局集动画化和移动按钮的对话框:

    [!https://gitlab.com/gradha/stackoverflow53343622/wikis/uploads/8e7a47d939677277459619db6af27de3/mi8_buttons.gif]

    1 回复  |  直到 6 年前
        1
  •  1
  •   Javier Rebollo    6 年前

    最好的解决方案是动画视图,而不是片段、对话框或您正在使用的任何内容的转换。