【问题标题】:how to change the alertdialog box style in android如何在android中更改警报对话框样式
【发布时间】:2016-04-05 19:21:51
【问题描述】:

我只是想为我的 android 应用程序构建一个警报对话框。

我真正想要的是这样一个对话框:

其正负按钮在对话框中填充

但我总是得到这样的结果:

这两个按钮在对话框的右上角粘在一起

谁能告诉我如何将 alertdialog 从第二个更改为第一个?

非常感谢

【问题讨论】:

  • 为你的对话框使用这个主题。
  • 你是如何创建对话框的?请出示您的代码
  • 其实没用

标签: android android-alertdialog


【解决方案1】:

为此,您需要像这样创建自定义对话框 对话框 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:contentDescription="@drawable/ic_launcher"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image"
    android:layout_centerHorizontal="true"
    android:text="Dismiss" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@+id/button"
    android:layout_toRightOf="@+id/image"
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

在你的活动中:

final Dialog dialog = new Dialog(MainActivity.this);

            //setting custom layout to dialog
            dialog.setContentView(R.layout.cusotm_dialog_layout);
            dialog.setTitle("Custom Dialog");

            //adding text dynamically
            TextView txt = (TextView) dialog.findViewById(R.id.textView);
            txt.setText("Put your dialog text here.");

            ImageView image = (ImageView)dialog.findViewById(R.id.image);
            image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));

            //adding button click event
            Button dismissButton = (Button) dialog.findViewById(R.id.button);
            dismissButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();

【讨论】:

    【解决方案2】:

    您可以使用 setView(view) 方法将自定义布局设置为警报对话框,如下所示:

     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
     LayoutInflater inflater = this.getLayoutInflater();
     View dialogView = inflater.inflate(R.layout.alert_layout, null);
     dialogBuilder.setView(dialogView);
     AlertDialog alertDialog = dialogBuilder.create();
     alertDialog.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-08
      • 2015-08-17
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多