問題描述
我在我的Activity中做了一個簡單的AlertDialog
:
I made a simple AlertDialog
in my Activity:
View view = layoutInflater.inflate(R.layout.my_dialog, null);
AlertDialog infoDialog = new AlertDialog.Builder(MyActivity.this)
.setView(view)
.create();
infoDialog.show();
使用上面的代碼,對話框顯示在屏幕的(大約)中心.
With above code, the dialog shows at the (about) the center of the screen.
我想知道,如何自定義對話框位置以使其顯示在頂部操作欄下方?(無論如何要改變對話框的重力或其他東西?)以及如何根據我的代碼來做到這一點??
I am wondering, how can I customize the dialog position to make it showing just under the top Action Bar ? (Is there anyway to change the gravity or something of the dialog?) and how to do it based on my code??
推薦答案
我用這段代碼在屏幕底部顯示對話框:
I used this code to show the dialog at the bottom of the screen:
Dialog dlg = <code to create custom dialog>;
Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
如果需要,此代碼還可以防止 android 使對話框的背景變暗.您應該能夠更改重力參數以移動對話框
This code also prevents android from dimming the background of the dialog, if you need it. You should be able to change the gravity parameter to move the dialog about
這篇關于在屏幕android上更改對話框的位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!