我创建了一个警报对话框,我想知道如何将消息的严重性设置为中心:
Dialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(completedMessage());
dialog = builder.create();
这是completedMessage()方法:
private String completedMessage() {
String[] messages = getResources().getStringArray(R.array.success);
String completeMsg = "\n";
int messagei= (int) Math.floor(Math.random() * messages.length);
completeMsg += messages[messagei];
return completeMsg;
}
这是arrays.success:
<string-array name="success">
<item>You got it!</item>
<item>Success!</item>
<item>Done!</item>
<item>Great job, young Padawan.</item>
</string-array>
没有标题,没有按钮,没有列表,只有消息,我想将它显示在中心,无论字符串长度。
我试过了:
Dialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(completedMessage());
dialog = builder.show();
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER_VERTICAL);
但这没有任何改变。
顺便说一下,信息显示在框中,就像在顶部和左边有一个填充,但是底部和右边都没有填充。
答案 0 :(得分:2)
试试这个
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My Title");
builder.setMessage("your message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.show();