問題描述
當我在布局中使用時,它指定了這個對話框 android:layout_width="match_parent"
我得到了這個對話框:
When I use in layout, which specifies this dialog android:layout_width="match_parent"
I get this dialog:
我需要更寬的對話框.有什么想法嗎?
I need dialog which will be wider. Any ideas?
推薦答案
您可以通過抓取對話框使用的 Window
對象并重置寬度來實現.這是一個簡單的例子:
You can do that by grabbing the Window
object that the dialog uses, and resetting the width. Here's a simple example:
//show the dialog first
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Test Dialog")
.setMessage("This should expand to the full width")
.show();
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
這是最終結果.請注意,如果您需要微調某些內容(例如更改背景等),您可以對對話框進行更多樣式設置.當我過去不得不這樣做時,我通常使用這種方法,并在構建器類上使用 setView()
自定義對話框中使用的布局.
Here's the end result. Note that there's a lot more styling you can do to the dialog if you need to fine tune things (like change the background, etc). When I've had to do this in the past, I usually use this method, and customize the layout used in the dialog with setView()
on the builder class.
這篇關于如何創建將在水平維度上充滿的對話框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!