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

我尝试了以下几点。

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

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

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

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

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

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


当前回答

导入自定义警报:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dse_location_list_filter, null);
final Dialog dialog = new Dialog(Acitvity_name.this);
dialog.setContentView(view);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.show();

其他回答

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

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>

我发现这是显示自定义对话框的最简单方法。

你有布局your_layout。xml

public void showCustomDialog(final Context context) {
    Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.your_layout, null, false);
    findByIds(view);  /*HERE YOU CAN FIND YOU IDS AND SET TEXTS OR BUTTONS*/
    ((Activity) context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    dialog.setContentView(view);
    final Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setBackgroundDrawableResource(R.color.colorTransparent);
    window.setGravity(Gravity.CENTER);
    dialog.show();
}

创建自定义警报布局custom_aler_update.xml

然后将此代码复制到Activity:

AlertDialog basic_reg;
AlertDialog.Builder builder ;
builder = new AlertDialog.Builder(ct, R.style.AppCompatAlertDialogStyle);
LayoutInflater inflater = ((Activity) ct).getLayoutInflater();
View v = inflater.inflate(R.layout.custom_aler_update, null);
builder.setView(v);
builder.setCancelable(false);
builder.create();
basic_reg = builder.show();

复制此代码到样式:

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@color/primaryTextColor</item>
    <item name="android:background">@color/white</item>
</style>
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();

            }
        });

    }

全屏自定义警报对话框类在Kotlin

Create XML file, same as you would an activity Create AlertDialog custom class class Your_Class(context:Context) : AlertDialog(context){ init { requestWindowFeature(Window.FEATURE_NO_TITLE) setCancelable(false) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.your_Layout) val window = this.window window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT) //continue custom code here //call dismiss() to close } } Call the dialog within the activity val dialog = Your_Class(this) //can set some dialog options here dialog.show()

注意**:如果您不希望对话框全屏显示,请删除以下行

      val window = this.window
      window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                         WindowManager.LayoutParams.MATCH_PARENT)

然后在XML文件中编辑顶部布局的layout_width和layout_height,使其为wrap_content或固定的DP值。

我通常不建议使用固定的DP,因为你可能希望你的应用能适应多种屏幕尺寸,但如果你保持你的尺寸值足够小,你应该没问题