为什么我的对话框布局无法正确传播?

时间:2019-04-19 11:00:43

标签: android android-layout android-constraintlayout

在我的布局(自定义对话框类的布局)中,每当我这样设置布局属性时:

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 个答案:

答案 0 :(得分:1)

您可以通过将LayoutParams的{​​{1}}设置为所需的目标

这是代码

Dialog

答案 1 :(得分:1)

尝试这种方式

aws ec2 describe-instances --output table --instance-id youInstanceID

答案 2 :(得分:0)

请在对话框中添加动态宽度和高度。您可以为此使用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);

答案 3 :(得分:0)

请您输入以下代码: 以编程方式设置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();
        }
    }