如何将此对话框的自定义高度和宽度直接设置到主 Activity 文件中。我尝试了一种方法,但它只匹配父级的高度和宽度
public void onBackPressed()
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.app_name);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Please Help Us Grow before leaving!")
.setCancelable(false)
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("Share our app", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Download it now: https://play.google.com/store/apps/details?id=com.app.package");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(alertDialog.getWindow().getAttributes());
layoutParams.width = WindowManager.LayoutParams(100);
layoutParams.height = WindowManager.LayoutParams(100);
alertDialog.getWindow().setAttributes(layoutParams);
最佳答案
您可以将其设置为alertDialog.getWindow().setLayout(600, 400);
也可以将其设置为
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(alertDialog.getWindow().getAttributes());
layoutParams.width = 150;
layoutParams.height = 500;
layoutParams.x=-180;
layoutParams.y=100;
alertDialog.getWindow().setAttributes(layoutParams);
关于java - 如何在android中设置自定义对话框的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62421473/