我想创建一个自定义对话框如下

我尝试了以下几点。

我创建了AlertDialog的子类。生成器,并使用自定义标题和自定义内容视图,并使用,但结果不是预期的。 另一个尝试是子类DialogFragment和自定义对话框内的onCreateDialog,但结果不是预期的。 然后我尝试使用一个普通的Dialog类。结果并不如预期的那样。

在这三种情况下,问题是当我忽略标题视图时,对话框的大小不是预期的,当我使用标题视图时,结果是在内容视图周围有一个粗边框(这看起来真的很糟糕)。现在我脑子里有两个问题……

我怎样才能做到呢?因为我已经尝试了很多事情,直接的回答会更受欢迎。 在android应用程序中显示错误或警告对话框的最佳方式是什么?

编辑 Android开发者文档建议我们应该使用对话片段或对话框向用户显示错误/警报消息。然而,他们一度说……

提示:如果你想要一个自定义对话框,你可以将一个Activity显示为一个对话框,而不是使用对话框api。简单地创建一个活动,并将其主题设置为manifest元素中的theme . holo . dialog。

这是什么意思?仅仅是为了显示错误消息而使用Activity不是太过分了吗??


当前回答

创建自定义对话框的最简单方法:

Initialize and show dialog: ViewDialog alertDialoge = new ViewDialog(); alertDialoge.showDialog(getActivity(), "PUT DIALOG TITLE"); Create method: public class ViewDialog { public void showDialog(Activity activity, String msg) { final Dialog dialog = new Dialog(activity); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCancelable(false); dialog.setContentView(R.layout.custom_dialoge_feedback); TextView text = (TextView) dialog.findViewById(R.id.text_dialog_feedback); text.setText(msg); Button okButton = (Button) dialog.findViewById(R.id.btn_dialog_feedback); Button cancleButton = (Button) dialog.findViewById(R.id.btn_dialog_cancle_feedback); final EditText edittext_tv = (EditText) dialog.findViewById(R.id.dialoge_alert_text_feedback); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Perfome Action } }); cancleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.show(); } } Create layout XML which you want or need.

其他回答

你可以尝试这个简单的android对话框弹出库来减少混乱的对话框代码。在您的活动中使用它非常简单。 在此之后,您可以在活动中使用此代码来显示对话框

Pop.on(this).with().title(R.string.title).layout(R.layout.custom_pop).show();

出来的地方。Custom_pop是你想要装饰对话框的自定义布局。

创建自定义警报对话框

cumstomDialog.xml

<ImageView
    android:id="@+id/icon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    app:srcCompat="@drawable/error" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:fontFamily="@font/muli_bold"
    android:text="Title"
    android:layout_marginTop="5dp"
    android:textColor="@android:color/black"
    android:textSize="15sp" />


<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:fontFamily="@font/muli_regular"
    android:text="Message"
    android:textColor="@android:color/black"
    android:textSize="12dp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/cancelBTN"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_margin="5dp"
        android:background="@drawable/bground_radius_button_white"
        android:text="No"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/acceptBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="5dp"
        android:background="@drawable/bground_radius_button"
        android:text="Yes"
        android:textColor="@color/white" />
</LinearLayout>

在你的活动上显示你的自定义对话框:

  public void showDialog(String title, String des, int icon) {

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    Button cancelBTN = dialog.findViewById(R.id.cancelBTN);
    Button acceptBTN = dialog.findViewById(R.id.acceptBtn);
    TextView tvTitle = dialog.findViewById(R.id.title);
    TextView tvDescription = dialog.findViewById(R.id.description);
    ImageView ivIcon = dialog.findViewById(R.id.icon);

    tvTitle.setText(title);
    tvDescription.setText(des);
    ivIcon.setImageResource(icon);

    cancelBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    acceptBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    dialog.show();
}

像这样调用:

“标题”、“信息”、“警告”;

创建自定义对话框的最简单方法:

Initialize and show dialog: ViewDialog alertDialoge = new ViewDialog(); alertDialoge.showDialog(getActivity(), "PUT DIALOG TITLE"); Create method: public class ViewDialog { public void showDialog(Activity activity, String msg) { final Dialog dialog = new Dialog(activity); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCancelable(false); dialog.setContentView(R.layout.custom_dialoge_feedback); TextView text = (TextView) dialog.findViewById(R.id.text_dialog_feedback); text.setText(msg); Button okButton = (Button) dialog.findViewById(R.id.btn_dialog_feedback); Button cancleButton = (Button) dialog.findViewById(R.id.btn_dialog_cancle_feedback); final EditText edittext_tv = (EditText) dialog.findViewById(R.id.dialoge_alert_text_feedback); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Perfome Action } }); cancleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.show(); } } Create layout XML which you want or need.

public static void showCustomAlertDialog(Context context, String name,
            String id, String desc, String fromDate, String toDate,
            String resions) {
        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.dialog, null);
        alertDialogBuilder.setView(view);
        alertDialogBuilder.setCancelable(false);
        final AlertDialog dialog = alertDialogBuilder.create();
        dialog.show();
        txt_empId = (TextView) view.findViewById(R.id.txt_dialog_empcode);
        txt_empName = (TextView) view.findViewById(R.id.txt_dialog_empname);
        txt_desc = (TextView) view.findViewById(R.id.txt_dialog_desc);
        txt_startDate = (TextView) view.findViewById(R.id.txt_dialog_startDate);
        txt_resions = (TextView) view.findViewById(R.id.txt_dialog_endDate);
        txt_empId.setTypeface(Utils.setLightTypeface(context));
        txt_empName.setTypeface(Utils.setLightTypeface(context));
        txt_desc.setTypeface(Utils.setLightTypeface(context));
        txt_startDate.setTypeface(Utils.setLightTypeface(context));
        txt_resions.setTypeface(Utils.setLightTypeface(context));

        txt_empId.setText(id);
        txt_empName.setText(name);

        txt_desc.setText(desc);
        txt_startDate.setText(fromDate + "\t to \t" + toDate);
        txt_resions.setText(resions);



        btn_accept = (Button) view.findViewById(R.id.btn_dialog_accept);
        btn_reject = (Button) view.findViewById(R.id.btn_dialog_reject);
        btn_cancel = (Button) view.findViewById(R.id.btn_dialog_cancel);
        btn_accept.setTypeface(Utils.setBoldTypeface(context));
        btn_reject.setTypeface(Utils.setBoldTypeface(context));
        btn_cancel.setTypeface(Utils.setBoldTypeface(context));

        btn_cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.dismiss();

            }
        });

    }

用这段代码正确显示自定义对话框的简单方法

dialog_choose_photo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/l_buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/_5sdp"
    android:layout_marginRight="@dimen/_5sdp"
    android:layout_marginBottom="@dimen/_5sdp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_dialog_smallround_white"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_camera"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:paddingTop="@dimen/_10sdp"
            android:paddingBottom="@dimen/_10sdp"
            android:text="Take a Photo"
            android:textColor="#3080C7"
            android:textSize="18sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#F1F0F0" />

        <TextView
            android:id="@+id/txt_gallery"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:paddingTop="@dimen/_10sdp"
            android:paddingBottom="@dimen/_10sdp"
            android:text="Choose from Gallery"
            android:textColor="#3080C7"
            android:textSize="18sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_5sdp"
        android:background="@drawable/bg_dialog_smallround_white"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt_cancel_dialog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:paddingTop="@dimen/_10sdp"
            android:paddingBottom="@dimen/_10sdp"
            android:text="Cancel"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />
    </LinearLayout>


</LinearLayout>


</RelativeLayout>

只要调用这个函数

 public void openPhotoDialog() {
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_choose_photo);
    dialog.setCancelable(true);

    // Setting dialogview

    final Window window = dialog.getWindow();
    window.setLayout(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    WindowManager.LayoutParams wlp = window.getAttributes();

    wlp.gravity = Gravity.BOTTOM;
    window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    window.setDimAmount(0.5f);
    window.setAttributes(wlp);


    TextView txt_camera = dialog.findViewById(R.id.txt_camera);
    TextView txt_gallery = dialog.findViewById(R.id.txt_gallery);
    TextView txt_cancel_dialog = dialog.findViewById(R.id.txt_cancel_dialog);

    txt_camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            dialog.dismiss();
        }
    });
    txt_gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            dialog.dismiss();
        }
    });
    txt_cancel_dialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            dialog.dismiss();
        }
    });

    dialog.show();

}

Bg_dialog_smallround_white.xml绘制资源

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />

<corners
    android:radius="@dimen/_5sdp" />
</shape>