AlertDialog 没有正确显示我想要的对话框
AlertDialog not Presenting correctly the dialog i wanted
我正在尝试在我的活动中显示一个警告对话框,它描述了如果用户没有在我的数据库中存储一些信息,那么一个警告对话框将在用户面前弹出一个按钮。
这是我在 activity
中的对话框代码
AlertDialog.Builder myAlert= new AlertDialog.Builder(this);
myAlert.setMessage("You are winner!").setPositiveButton("Continue...", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setTitle("Welcome")
.create();
myAlert.show();
所以基本上我没有得到我想要的警报对话框,我得到的是这样的东西 enter image description here。
我的标题、消息和按钮之间没有边框,我的按钮默认没有放在中间,我应该怎么做才能解决这些问题我已经尝试了很多视频和帖子。
您应该为此使用自定义对话框。创建自定义布局。并将layoutview设置到对话框。
示例代码
button = (Button) findViewById(R.id.buttonShowCustomDialog);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
详情见下方link
http://www.mkyong.com/android/android-custom-dialog-example/
试试这个:
AlertDialog.Builder myAlert= new AlertDialog.Builder(this, android.R.style.Theme_Holo_Light_Dialog);
myAlert.setTitle("Welcome")
.setMessage("You are winner!")
.setCancelable(false)
.setPositiveButton("Continue...", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = myAlert.create();
alert.show();
i have no borders between title and message and button, my button not
placed at center as default ,what should i do to fix these i have
tried many videos and posts.
如果您不喜欢 material 对话框并且想要平台的特定对话框,那么您应该使用 android.app.AlertDialog 代替android.support.v7.app.AlertDialog.
将导入语句从 android.support.v7.app.AlertDialog 更改为 android.app.AlertDialog
但我建议您使用 material 对话框。
我正在尝试在我的活动中显示一个警告对话框,它描述了如果用户没有在我的数据库中存储一些信息,那么一个警告对话框将在用户面前弹出一个按钮。 这是我在 activity
中的对话框代码AlertDialog.Builder myAlert= new AlertDialog.Builder(this);
myAlert.setMessage("You are winner!").setPositiveButton("Continue...", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setTitle("Welcome")
.create();
myAlert.show();
所以基本上我没有得到我想要的警报对话框,我得到的是这样的东西 enter image description here。
我的标题、消息和按钮之间没有边框,我的按钮默认没有放在中间,我应该怎么做才能解决这些问题我已经尝试了很多视频和帖子。
您应该为此使用自定义对话框。创建自定义布局。并将layoutview设置到对话框。
示例代码
button = (Button) findViewById(R.id.buttonShowCustomDialog);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
详情见下方link
http://www.mkyong.com/android/android-custom-dialog-example/
试试这个:
AlertDialog.Builder myAlert= new AlertDialog.Builder(this, android.R.style.Theme_Holo_Light_Dialog);
myAlert.setTitle("Welcome")
.setMessage("You are winner!")
.setCancelable(false)
.setPositiveButton("Continue...", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = myAlert.create();
alert.show();
i have no borders between title and message and button, my button not placed at center as default ,what should i do to fix these i have tried many videos and posts.
如果您不喜欢 material 对话框并且想要平台的特定对话框,那么您应该使用 android.app.AlertDialog 代替android.support.v7.app.AlertDialog.
将导入语句从 android.support.v7.app.AlertDialog 更改为 android.app.AlertDialog
但我建议您使用 material 对话框。