猿问

在屏幕的任何位置显示AlertDialog

在屏幕的任何位置显示AlertDialog

当我们在android中显示AlertDialog时,它显示在屏幕的中心。有没有办法改变立场?



郎朗坤
浏览 554回答 3
3回答

慕雪6442864

这些答案将移动AlertDialog的位置,但是,显示的对话框的位置还将包括对话框周围的填充。如果你想摆脱这个填充(例如,将对话框放在屏幕底部),你还需要覆盖styles.xml中的默认AlertDialog样式,将windowBackground设置为null,就像这样:<resources> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;&nbsp;Example&nbsp;app&nbsp;theme&nbsp;-&nbsp;mine&nbsp;uses&nbsp;the&nbsp;below&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;<style&nbsp;name="AppTheme"&nbsp;parent="Theme.AppCompat.Light.NoActionBar"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:alertDialogTheme">@style/MyDialogTheme</item> &nbsp;&nbsp;&nbsp;&nbsp;</style> &nbsp;&nbsp;&nbsp;&nbsp;<style&nbsp;name="MyDialogTheme"&nbsp;parent="Theme.AppCompat.Light.Dialog.Alert"> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;Full&nbsp;width&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:layout_width">fill_parent</item> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;Null&nbsp;window&nbsp;background&nbsp;kills&nbsp;surrounding&nbsp;padding&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:windowBackground">@null</item> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:windowNoTitle">true</item> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</style></resources>以及如接受的答案中所述设置Window.LayoutParameters。特别向@David Caunt大喊大叫,他的回答是:删除边框,从Dialog填充完成了这张照片。

白板的微信

为了使设置产生信息效果,我添加了以下代码dialog.getWindow().setAttributes(wmlp);在gypsicoder的答案中更改了wmlp的值,或者我的测试没有生效wmlp的设置。
随时随地看视频慕课网APP

相关分类

Android
我要回答