【问题标题】:How to creat Dialog PopUp window with custom Layout?如何使用自定义布局创建对话框弹出窗口?
【发布时间】:2020-02-23 10:02:36
【问题描述】:

我正在尝试使用自定义布局创建对话框弹出窗口,但我的自定义布局高度和弹出窗口高度不匹配。此外,弹出窗口隐藏了一个按钮。我该怎么办,如何将我的弹出窗口高度与自定义布局相匹配? 请帮我。 我希望我的弹出窗口看起来像这样

但它看起来像这样

我的自定义布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="336dp"
    android:layout_height="156dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom|center"
    android:background="@drawable/rectangle"
    android:layout_marginBottom="88dp">


    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:layout_marginStart="58dp"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/bikepic" />

<ImageView
    android:id="@+id/imageView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="64dp"
    android:layout_marginBottom="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/delivery" />

<TextView
    android:id="@+id/biketextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="69dp"
    android:layout_marginTop="17dp"
    android:background="@drawable/biketextview"
    android:gravity="center"
    android:text="Bike"
    android:textColor="#FFFFFF"
    android:textSize="12sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/deliverytextView"
    android:layout_width="64dp"
    android:layout_height="18dp"
    android:layout_marginTop="17dp"
    android:layout_marginEnd="64dp"
    android:background="@drawable/deliverytextview"
    android:gravity="center"
    android:text="Delivery"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="23dp"
    android:layout_height="14dp"
    android:layout_marginStart="78dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:text="74"
    android:textSize="12sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView5" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="23dp"
    android:layout_height="14dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="80dp"
    android:gravity="center"
    android:text="39"
    android:textSize="12sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView6" />

弹出对话框代码

popAddPost = new Dialog(this);
    popAddPost.setContentView(R.layout.pop_up_confirm_pickup);
    //popAddPost.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    //popAddPost.getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT,Toolbar.LayoutParams.WRAP_CONTENT);
    popAddPost.getWindow().getAttributes().gravity = Gravity.BOTTOM;

【问题讨论】:

    标签: java android dialog popupwindow


    【解决方案1】:

    在您的 xml 中,父布局在 ConstraintLayout 上使用 RelativeLayout LayoutParams

    删除android:layout_alignParentLeft="true" 和android:layout_alignParentEnd="true"

    ConstraintLayout 也不支持layout_gravity,所以你也可以删除它。

    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/rectangle"
        android:layout_marginBottom="88dp">
    

    代码

    popAddPost = new Dialog(this);
    popAddPost.setContentView(R.layout.pop_up_confirm_pickup);
    Objects.requireNonNull(popAddPost.getWindow()).setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    popAddPost.show();
    

    编辑

    要将对话框定位在底部,请使用:

    Window window = popAddPost.getWindow();
    WindowManager.LayoutParams params = window.getAttributes();
    params.gravity = Gravity.BOTTOM;
    window.setAttributes(params);
    

    【讨论】:

      【解决方案2】:

      这就是我的做法:

      private AlertDialog alertDialog;
      alertDialog = buildDialog();
      alertDialog.show();
      
      private AlertDialog buildDialog(){
              final AlertDialog dialogBuilder = new AlertDialog.Builder(context).create();
              LayoutInflater inflater = LayoutInflater.from(context);
              View dialogView = inflater.inflate(R.layout.dialog, null);
      
              dialogBuilder.setView(dialogView);
      
              dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE, "CLOSE", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      dialogBuilder.dismiss();
                  }
              });
              return dialogBuilder;
          }
      

      【讨论】:

        【解决方案3】:

        我认为对话框不是解决您的问题的好方法,因为对话框会有自己的带有叠加层的窗口。

        正如我所见,您希望对话框位于屏幕底部,这使得 BottomSheet 非常适合您的问题。 您可以使用以下方法开始:

        https://www.androidhive.info/2017/12/android-working-with-bottom-sheet/

        https://material.io/develop/android/components/bottom-sheet-behavior/

        https://medium.com/@droidbyme/android-bottom-sheet-7e9cfcec6427

        使用 BottomSheet,您将获得一个单独的窗口,就像没有您所面对的叠加层的对话框一样。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-11
          相关资源
          最近更新 更多