Why my dialog layout wont spread correctly?

心已入冬 提交于 2021-02-11 12:35:05

问题


In my layout (its layout for custom dialog class), whenever I set my layout attributes like this :

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

I don't see a layout that spreads all over the screen, I see it like this:

As you can see, the dialog is very small and weird looking although I am using match_parent, the weird part is that whenever I use fixed size I see it normally.

For example - for those attributes:

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

I see it like this:

Do you have any idea why this is happening or if I am doing something wrong? I don't want to use a fixed size because that will make my layout non-responsive.

Here is my layout file :

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

回答1:


You can achieve what you want with setting LayoutParams of Dialog

Here is the code

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



回答2:


Try this way

Window window = yourDialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
yourDialog.show();



回答3:


Please add dynamic Width and Height to your Dialog. You can use LayoutParams for that.

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



回答4:


Please you this code: Set programmatically ConstraintLayout height and width

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 File:

Set runtime height and width

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

getDeviceWidth Method:

@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();
        }
    }


来源:https://stackoverflow.com/questions/55760811/why-my-dialog-layout-wont-spread-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!