Почему мой макет диалога не распространяется правильно? - PullRequest
0 голосов
/ 19 апреля 2019

В моем макете (его макет для пользовательского диалогового класса) всякий раз, когда я устанавливаю свои атрибуты макета следующим образом:

android:layout_width="match_parent"
android:layout_height="match_parent"

Я не вижу макет, который распространяется по всему экрану, я вижу егокак это:

enter image description here

Как видите, диалоговое окно очень маленькое и выглядит странно, хотя я использую match_parent, странная часть в том, чтовсякий раз, когда я использую фиксированный размер, я вижу его нормально.

Например, - для этих атрибутов:

android:layout_width="300dp"
android:layout_height="300dp"

Я вижу это так:

enter image description here

У вас есть идеи, почему это происходит или я делаю что-то не так?Я не хочу использовать фиксированный размер, потому что это сделает мой макет не отвечающим.

Вот мой файл макета:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layoutDirection="ltr">


<TextView
    android:id="@+id/titleAchievement"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="@string/achievment"
    android:textAlignment="center"
    android:textColor="@color/primaryColor"
    android:textSize="20sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/theScoreIs"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="@string/theScoreIs"
    android:textAlignment="center"
    android:textColor="@color/primaryColor"
    android:textSize="20sp"
    app:layout_constraintBottom_toTopOf="@+id/guideline14"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/titleAchievement" />


<TextView
    android:id="@+id/textView4"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="8dp"
    android:paddingLeft="16dp"
    android:text="@string/bronze"
    android:textAlignment="center"
    android:textColor="@color/bronze"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/progress_1"
    app:layout_constraintEnd_toStartOf="@+id/guideline16"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/progress_1"
    app:layout_constraintVertical_chainStyle="spread_inside" />

<ImageView
    android:id="@+id/icon1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/textView4"
    app:layout_constraintEnd_toStartOf="@+id/guideline13"
    app:layout_constraintStart_toStartOf="@+id/guideline16"
    app:layout_constraintTop_toTopOf="@+id/textView4" />

<com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar
    android:id="@+id/progress_1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="24dp"
    app:layout_constraintBottom_toTopOf="@+id/progress_2"
    app:layout_constraintEnd_toStartOf="@+id/guideline15"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="@+id/guideline13"
    app:layout_constraintTop_toBottomOf="@+id/theScoreIs" />


<TextView
    android:id="@+id/textView5"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:paddingLeft="16dp"
    android:text="@string/silver"
    android:textAlignment="center"
    android:textColor="@color/silver"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/progress_2"
    app:layout_constraintEnd_toEndOf="@+id/textView4"
    app:layout_constraintHorizontal_bias="0.355"
    app:layout_constraintStart_toStartOf="@+id/textView4"
    app:layout_constraintTop_toTopOf="@+id/progress_2" />

<ImageView
    android:id="@+id/icon2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/textView5"
    app:layout_constraintEnd_toEndOf="@+id/icon1"
    app:layout_constraintStart_toStartOf="@+id/icon1"
    app:layout_constraintTop_toTopOf="@+id/textView5" />

<com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar
    android:id="@+id/progress_2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="23dp"
    app:layout_constraintBottom_toTopOf="@+id/progress_3"
    app:layout_constraintEnd_toEndOf="@+id/progress_1"
    app:layout_constraintStart_toStartOf="@+id/guideline13"
    app:layout_constraintTop_toBottomOf="@+id/progress_1" />


<TextView
    android:id="@+id/textView6"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:paddingLeft="16dp"
    android:text="@string/gold"
    android:textAlignment="center"
    android:textColor="@color/gold"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/progress_3"
    app:layout_constraintEnd_toEndOf="@+id/textView5"
    app:layout_constraintStart_toStartOf="@+id/textView5"
    app:layout_constraintTop_toTopOf="@+id/progress_3" />

<ImageView
    android:id="@+id/icon3"
    android:layout_width="0dp"

    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/textView6"
    app:layout_constraintEnd_toEndOf="@+id/icon1"
    app:layout_constraintStart_toStartOf="@+id/icon1"
    app:layout_constraintTop_toTopOf="@+id/textView6" />

<com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar
    android:id="@+id/progress_3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="24dp"
    app:layout_constraintBottom_toTopOf="@+id/progress_4"
    app:layout_constraintEnd_toEndOf="@+id/progress_1"
    app:layout_constraintStart_toStartOf="@+id/guideline13"
    app:layout_constraintTop_toBottomOf="@+id/progress_2" />


<TextView
    android:id="@+id/textView8"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:paddingLeft="16dp"
    android:text="@string/master"
    android:textAlignment="center"
    android:textColor="@color/master"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="@+id/progress_4"
    app:layout_constraintEnd_toEndOf="@+id/textView4"
    app:layout_constraintStart_toStartOf="@+id/textView4"
    app:layout_constraintTop_toTopOf="@+id/progress_4" />

<ImageView
    android:id="@+id/icon4"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/textView8"
    app:layout_constraintEnd_toEndOf="@+id/icon1"
    app:layout_constraintStart_toStartOf="@+id/icon1"
    app:layout_constraintTop_toTopOf="@+id/textView8" />

<com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar
    android:id="@+id/progress_4"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="26dp"
    app:layout_constraintBottom_toTopOf="@+id/closeDiag"
    app:layout_constraintEnd_toEndOf="@+id/progress_1"
    app:layout_constraintStart_toStartOf="@+id/guideline13"
    app:layout_constraintTop_toBottomOf="@+id/progress_3" />


<Button
    android:id="@+id/closeDiag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:background="@drawable/main_color_background"
    android:text="@string/ok"
    android:textColor="@color/secondaryTextColor"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

<android.support.constraint.Guideline
    android:id="@+id/guideline16"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.31" />

<android.support.constraint.Guideline
    android:id="@+id/guideline15"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="1.0" />

<android.support.constraint.Guideline
    android:id="@+id/guideline14"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.15" />

</android.support.constraint.ConstraintLayout>

Ответы [ 4 ]

1 голос
/ 19 апреля 2019

Попробуйте вот так

Window window = yourDialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
yourDialog.show();
1 голос
/ 19 апреля 2019

Вы можете достичь желаемого, установив LayoutParams из Dialog

Вот код

Window window = yourDialog.getWindow();
if (window != null) {
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(window.getAttributes());
    //This makes the dialog take up the full width
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    window.setAttributes(lp);
}
0 голосов
/ 19 апреля 2019

Пожалуйста, вам этот код: Установить программно ConstraintLayout высоту и ширину

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/LiMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/titleAchievement"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="@string/achievment"
            android:textAlignment="center"
            android:textColor="@color/primaryColor"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/theScoreIs"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="@string/theScoreIs"
            android:textAlignment="center"
            android:textColor="@color/primaryColor"
            android:textSize="20sp"
            app:layout_constraintBottom_toTopOf="@+id/guideline14"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/titleAchievement" />


        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="8dp"
            android:paddingLeft="16dp"
            android:text="@string/bronze"
            android:textAlignment="center"
            android:textColor="@color/bronze"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@+id/progress_1"
            app:layout_constraintEnd_toStartOf="@+id/guideline16"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/progress_1"
            app:layout_constraintVertical_chainStyle="spread_inside" />

        <ImageView
            android:id="@+id/icon1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/textView4"
            app:layout_constraintEnd_toStartOf="@+id/guideline13"
            app:layout_constraintStart_toStartOf="@+id/guideline16"
            app:layout_constraintTop_toTopOf="@+id/textView4" />

        <com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar
            android:id="@+id/progress_1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="24dp"
            app:layout_constraintBottom_toTopOf="@+id/progress_2"
            app:layout_constraintEnd_toStartOf="@+id/guideline15"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="@+id/guideline13"
            app:layout_constraintTop_toBottomOf="@+id/theScoreIs" />


        <TextView
            android:id="@+id/textView5"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:paddingLeft="16dp"
            android:text="@string/silver"
            android:textAlignment="center"
            android:textColor="@color/silver"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@+id/progress_2"
            app:layout_constraintEnd_toEndOf="@+id/textView4"
            app:layout_constraintHorizontal_bias="0.355"
            app:layout_constraintStart_toStartOf="@+id/textView4"
            app:layout_constraintTop_toTopOf="@+id/progress_2" />

        <ImageView
            android:id="@+id/icon2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="@+id/textView5"
            app:layout_constraintEnd_toEndOf="@+id/icon1"
            app:layout_constraintStart_toStartOf="@+id/icon1"
            app:layout_constraintTop_toTopOf="@+id/textView5" />

        <com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar
            android:id="@+id/progress_2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="23dp"
            app:layout_constraintBottom_toTopOf="@+id/progress_3"
            app:layout_constraintEnd_toEndOf="@+id/progress_1"
            app:layout_constraintStart_toStartOf="@+id/guideline13"
            app:layout_constraintTop_toBottomOf="@+id/progress_1" />


        <TextView
            android:id="@+id/textView6"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:paddingLeft="16dp"
            android:text="@string/gold"
            android:textAlignment="center"
            android:textColor="@color/gold"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@+id/progress_3"
            app:layout_constraintEnd_toEndOf="@+id/textView5"
            app:layout_constraintStart_toStartOf="@+id/textView5"
            app:layout_constraintTop_toTopOf="@+id/progress_3" />

        <ImageView
            android:id="@+id/icon3"
            android:layout_width="0dp"

            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="@+id/textView6"
            app:layout_constraintEnd_toEndOf="@+id/icon1"
            app:layout_constraintStart_toStartOf="@+id/icon1"
            app:layout_constraintTop_toTopOf="@+id/textView6" />

        <com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar
            android:id="@+id/progress_3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="24dp"
            app:layout_constraintBottom_toTopOf="@+id/progress_4"
            app:layout_constraintEnd_toEndOf="@+id/progress_1"
            app:layout_constraintStart_toStartOf="@+id/guideline13"
            app:layout_constraintTop_toBottomOf="@+id/progress_2" />


        <TextView
            android:id="@+id/textView8"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:paddingLeft="16dp"
            android:text="@string/master"
            android:textAlignment="center"
            android:textColor="@color/master"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="@+id/progress_4"
            app:layout_constraintEnd_toEndOf="@+id/textView4"
            app:layout_constraintStart_toStartOf="@+id/textView4"
            app:layout_constraintTop_toTopOf="@+id/progress_4" />

        <ImageView
            android:id="@+id/icon4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="@+id/textView8"
            app:layout_constraintEnd_toEndOf="@+id/icon1"
            app:layout_constraintStart_toStartOf="@+id/icon1"
            app:layout_constraintTop_toTopOf="@+id/textView8" />

        <com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar
            android:id="@+id/progress_4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="26dp"
            app:layout_constraintBottom_toTopOf="@+id/closeDiag"
            app:layout_constraintEnd_toEndOf="@+id/progress_1"
            app:layout_constraintStart_toStartOf="@+id/guideline13"
            app:layout_constraintTop_toBottomOf="@+id/progress_3" />


        <Button
            android:id="@+id/closeDiag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:background="@drawable/main_color_background"
            android:text="@string/ok"
            android:textColor="@color/secondaryTextColor"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <android.support.constraint.Guideline
            android:id="@+id/guideline13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />

        <android.support.constraint.Guideline
            android:id="@+id/guideline16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.31" />

        <android.support.constraint.Guideline
            android:id="@+id/guideline15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="1.0" />

        <android.support.constraint.Guideline
            android:id="@+id/guideline14"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.15" />


    </android.support.constraint.ConstraintLayout>
</LinearLayout>

Файл Java:

Установить высоту и ширину среды выполнения

ConstraintLayout LiMain= dialogView.findViewById(R.id.LiMain);
        LiMain.setLayoutParams(new LinearLayout.LayoutParams(
                (int) (getDeviceWidth(context) / 1.3),
                (int) (getDeviceWidth(context) / 1.3)));

Метод getDeviceWidth:

@SuppressLint("NewApi")
    public int getDeviceWidth(Context context) {

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            Point point = new Point();
            wm.getDefaultDisplay().getSize(point);
            return point.x;
        } else {
            return wm.getDefaultDisplay().getWidth();
        }
    }
0 голосов
/ 19 апреля 2019

Пожалуйста, добавьте динамическую ширину и высоту в ваш диалог.Вы можете использовать LayoutParams для этого.

Dialog dialog = new Dialog(context);
dialog.show();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCancelable(true);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...