【问题标题】:white background under alert dialog box警告对话框下的白色背景
【发布时间】:2015-04-14 11:44:54
【问题描述】:

为什么我的警报对话框下会出现这个白色背景。我一直试图找出一个小时的问题,但没有运气。有人可以帮帮我吗?

还有,为什么标题的左右两边有点暗。

protected void onPostExecute(String result) {
    //progressDialog.dismiss();
    try {
        JSONObject json = new JSONObject(result);
        String status = json.getString("status");
        String message = json.getString("message");
        if(status.equals("true")) {
            Intent intent = new Intent(context, HomeActivity.class);
            intent.putExtra(LoginActivity.EXTRA_MESSAGE, status);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
        else{
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(message)
                   .setTitle("Error")
                   .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {
                           dialog.cancel();
                       }
                   }).create().show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 使用自定义对话框视图
  • 您在哪个模拟器或设备上运行您的应用程序?
  • 尝试设置主题,例如,AlertDialog.Builder(activity, android.R.style.Theme_Holo_Dialog);
  • @Harry,不工作我仍然得到白色背景。
  • @yaa110 我在 nexus 4 上运行

标签: android


【解决方案1】:

进口 android.support.v7.app.AlertDialog 而不是 android.app.AlertDialog

【讨论】:

    【解决方案2】:

    将您的代码更改为 -

     Dialog alertDialog = new Dialog(this);
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialog.setContentView(R.layout.tabs);
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        alertDialog.show();
    

    或者您可以将主题添加到现有代码中。

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
    

    【讨论】:

      【解决方案3】:

      初始化对话框生成器时,传递第二个参数作为主题。 所以改变

      AlertDialog.Builder builder = new AlertDialog.Builder(activity);
      

      AlertDialog.Builder builder = new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
      

      这是旧答案,现在

      导入android.support.v7.app.AlertDialog 而不是android.app.AlertDialog

      accepted answer 中所述。

      【讨论】:

      【解决方案4】:

      如果你可以访问 Dialog 类,试试这个:

       alertDialog.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);    
      

      之前:

      之后:

      【讨论】:

        猜你喜欢
        • 2013-01-07
        • 2017-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 1970-01-01
        相关资源
        最近更新 更多