问题
I want to display the alert dialog box to device screen size. I got the solution through this link How to make an alert dialog fill 90% of screen size? I use the solution given there, it works just fine but my alert message is displaying in very small size. we can't even make out the message in landscape orientation. I have used the following code.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.app_description).setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.Title(title);
Dialog d = builder.setView(new View(this)).create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
d.show();
d.getWindow().setAttributes(lp);
How to set the message, so that it will properly display in the dialog window?
Thanks in advance Pushpa
回答1:
Check this code and tell me if that's what you really wanted to achieve ?.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Creates textview
TextView text = new TextView(this);
text.setText("Hello This text");
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
text.setTextSize(20);
text.setGravity(Gravity.CENTER);
//Creates a linearlayout layout and sets it with initial params
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(text); //adds textview to llayout
builder.setMessage("Title").setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
Dialog d = builder.setView(ll).create();
//Fills up the entire Screen
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.FILL_PARENT;
d.show();
d.getWindow().setAttributes(lp);
回答2:
By the default implement-ion you can't set the text size. you can do one simple thing. wirte a XML and inflate that XML and pass to builder view.
builder.setView(view)
nothing but you are setting a view to dialog. and that view will handle all the touches. and you can specify height and width of the views in xml including text size.
来源:https://stackoverflow.com/questions/10432184/alert-message-is-not-displaying-in-alert-dialog-box