【问题标题】:AlertDialog not Presenting correctly the dialog i wantedAlertDialog 未正确显示我想要的对话框
【发布时间】:2015-10-14 11:40:42
【问题描述】:

我正在尝试在我的活动中显示一个警报对话框,它描述如果用户没有存储在我的数据库中的某些信息,那么将在用户面前弹出一个带有按钮的警报对话框。 这是我的活动对话框代码

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

我在标题、消息和按钮之间没有边界,我的按钮默认没有放在中心,我应该怎么做才能解决这些问题我已经尝试了很多视频和帖子。

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:

我在标题、消息和按钮之间没有边界,我的按钮没有 默认放置在中心,我应该怎么做才能解决这些问题 尝试了很多视频和帖子。

如果您不喜欢材质对话框并且想要平台的特定对话框,那么您应该使用 android.app.AlertDialog 代替 android.support.v7。 app.AlertDialog.

将导入语句从 android.support.v7.app.AlertDialog 更改为 android.app.AlertDialog

但我建议你使用材质对话框。

【讨论】:

  • 我更改了导入设置,它仍然给我相同的对话框。
  • 那么我猜你是在棒棒糖设备或模拟器上运行你的应用程序。
  • 我已经更改了 min sdk,我想我使用了其他主题,并且非常感谢。
【解决方案2】:

您应该为此使用自定义对话框。创建自定义布局。并 setlayoutview 到对话框。

示例代码

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();
      }
    });

详情见以下链接

http://www.mkyong.com/android/android-custom-dialog-example/

【讨论】:

    【解决方案3】:

    试试这个:

     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();
    

    【讨论】:

    • 我尝试使用您的代码行而不是全屏显示相同的对话框。
    • 它全屏打开对话框,不像我上传的图片那样弹出。
    • 那你要检查你使用的sdk版本和adt版本
    • android { compileSdkVersion 23 buildToolsVersion '23.0.1' defaultConfig { applicationId "com.example.barhom.waves" minSdkVersion 11 targetSdkVersion 23 versionCode 1 versionName "1.0" } 在这个问题上卡住了 1 天我解决不了...
    • dropbox.com/s/swz9so6botv6toc/… 我正在尝试不同的主题但没有得到正确的结果,我仍然看到标题、消息和按钮之间没有边框的对话框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2017-12-19
    • 2020-02-16
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多