sat*_*sat 70 android android-widget
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
Run Code Online (Sandbox Code Playgroud)
我使用上面的代码来显示警报对话框.默认情况下,它以宽度填充屏幕,高度填充wrap_content.
如何控制默认警报对话框的宽度和高度?
我试过了:
alert.getWindow().setLayout(100,100); // It didn't work.
Run Code Online (Sandbox Code Playgroud)
如何在警报窗口中获取布局参数并手动设置宽度和高度?
Piy*_*hra 186
只是在Sat Code中略有变化,设置show()方法后的布局AlertDialog.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
Run Code Online (Sandbox Code Playgroud)
或者你可以按我的方式做到这一点.
alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);
Run Code Online (Sandbox Code Playgroud)
sat*_*sat 27
好的,我可以使用Builder类控制宽度和高度.我用了
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
AlertDialog alertDialog = builder.create();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
alertDialog.show();
Run Code Online (Sandbox Code Playgroud)
tir*_*r38 15
在尝试调整布局后的大小之前,首先要检查对话框使用的样式.确保样式树中没有任何内容设置
<item name="windowMinWidthMajor">...</item>
<item name="windowMinWidthMinor">...</item>
Run Code Online (Sandbox Code Playgroud)
如果发生这种情况,就像为[构建器构造函数提供一个themeResId]提供自己的样式一样简单(http://developer.android.com/reference/android/app/AlertDialog.Builder.html#AlertDialog.Builder (android.content.Context,int))可用的API 11+
<style name="WrapEverythingDialog" parent=[whatever you were previously using]>
<item name="windowMinWidthMajor">0dp</item>
<item name="windowMinWidthMinor">0dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Pio*_*e98 10
这也适用于添加.getWindow().setLayout(width, height)之后show()
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
}).show().getWindow().setLayout(600,500);
Run Code Online (Sandbox Code Playgroud)
vov*_*888 10
对于那些会像我这样找到该线程的人,这是IMO更好的解决方案(请注意70%会以屏幕宽度的百分比设置对话框的最小宽度):
<style name="my_dialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="windowMinWidthMajor">70%</item>
<item name="windowMinWidthMinor">70%</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后将此样式应用于对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.my_dialog);
Run Code Online (Sandbox Code Playgroud)
如果要基于设备框架添加动态宽度和高度,则可以执行这些计算并指定高度和宽度。
AlertDialog.Builder builderSingle = new
AlertDialog.Builder(getActivity());
builderSingle.setTitle("Title");
final AlertDialog alertDialog = builderSingle.create();
alertDialog.show();
Rect displayRectangle = new Rect();
Window window = getActivity().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
alertDialog.getWindow().setLayout((int)(displayRectangle.width() *
0.8f), (int)(displayRectangle.height() * 0.8f));
Run Code Online (Sandbox Code Playgroud)
PS:首先显示对话框,然后尝试修改窗口的布局属性
感谢Sid 的回答,因为它是动态的,但我想添加一些内容。
如果您只想更改宽度,那么高度将保持原样。
我做了如下:
// All process of AlertDialog
AlertDialog alert = builder.create();
alert.show();
// Creating Dynamic
Rect displayRectangle = new Rect();
Window window = getActivity().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
alert.getWindow().setLayout((int) (displayRectangle.width() *
0.8f), alert.getWindow().getAttributes().height);
Run Code Online (Sandbox Code Playgroud)
在这里,我曾经alert.getWindow().getAttributes().height保持高度不变AlertDialog,Width并将根据屏幕分辨率进行更改。
希望它会有所帮助。谢谢。
| 归档时间: |
|
| 查看次数: |
138915 次 |
| 最近记录: |