Как создать всплывающее окно Dialog PopUp с пользовательским макетом? - PullRequest
0 голосов
/ 23 февраля 2020

Я пытаюсь создать всплывающее окно диалогового окна с пользовательским макетом, но высота моего пользовательского макета и высота всплывающего окна не совпадают. Также во всплывающем окне скрывается кнопка. Что я должен сделать, как я могу сопоставить высоту моего всплывающего окна с пользовательским макетом? Пожалуйста, помогите мне. Я хочу, чтобы мое всплывающее окно выглядело так

My goal

Но оно выглядит так

enter image description here

Мой пользовательский макет

<?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;

Ответы [ 3 ]

1 голос
/ 23 февраля 2020

В вашем xml родительский макет использует RelativeLayout LayoutParams для ConstraintLayout

Удалить 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);
1 голос
/ 23 февраля 2020

Я думаю, что диалог не является хорошим решением для вашей проблемы, так как у диалога будет собственное окно с оверлеем.

Как я вижу, вы хотите, чтобы диалог находился внизу экрана, что делает 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 вы получите отдельное окно, подобное диалогу, без наложения, с которым вы сталкиваетесь.

1 голос
/ 23 февраля 2020

Вот как я это делаю:

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;
    }
...