在自定义警报对话框上使用约束布局

时间:2018-05-23 05:56:57

标签: android-custom-view android-alertdialog android-constraintlayout

我正在尝试使用约束布局进行自定义警报对话(上帝知道为什么,我总是遇到它们有问题),我无法让视图显示任何内容,除非至少有一个元素谁的layout_height设置为包装内容和NOT匹配约束。以下是我的XML:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Spinner
        android:id="@+id/shapeSpinner"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/gH3"
        app:layout_constraintEnd_toStartOf="@+id/gV2"
        app:layout_constraintStart_toStartOf="@+id/gV1"
        app:layout_constraintTop_toTopOf="@+id/gHBreak">

    </Spinner>

    <android.support.constraint.Guideline
        android:id="@+id/guidelineSideLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.05" />
    <android.support.constraint.Guideline
        android:id="@+id/guidelineSideRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.95" />

    <android.support.constraint.Guideline
        android:id="@+id/gH1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />
    <android.support.constraint.Guideline
        android:id="@+id/gH2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.2" />
    <android.support.constraint.Guideline
        android:id="@+id/gHBreak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />
    <android.support.constraint.Guideline
        android:id="@+id/gH3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.35" />
    <android.support.constraint.Guideline
        android:id="@+id/gH4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />
    <android.support.constraint.Guideline
        android:id="@+id/gH5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.55" />
    <android.support.constraint.Guideline
        android:id="@+id/gEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.60" />

    <android.support.constraint.Guideline
        android:id="@+id/gV1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.35" />
    <android.support.constraint.Guideline
        android:id="@+id/gV2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.65" />

    <CheckBox
        android:id="@+id/shapeCheckBox"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Shape"
        app:layout_constraintBottom_toTopOf="@+id/gH1"
        app:layout_constraintEnd_toStartOf="@+id/gV1"
        app:layout_constraintStart_toStartOf="@+id/guidelineSideLeft"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/colourCheckBox"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Colour"
        app:layout_constraintBottom_toTopOf="@+id/gH1"
        app:layout_constraintEnd_toStartOf="@+id/gV2"
        app:layout_constraintStart_toStartOf="@+id/gV1"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/radiusCheckBox"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Radius"
        app:layout_constraintBottom_toTopOf="@+id/gH1"
        app:layout_constraintEnd_toStartOf="@+id/guidelineSideRight"
        app:layout_constraintStart_toStartOf="@+id/gV2"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/lWCheckBox"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Width/Height"
        app:layout_constraintBottom_toTopOf="@+id/gH2"
        app:layout_constraintEnd_toStartOf="@+id/gV1"
        app:layout_constraintStart_toStartOf="@+id/guidelineSideLeft"
        app:layout_constraintTop_toTopOf="@+id/gH1" />

    <CheckBox
        android:id="@+id/lengthCheckBox"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Length"
        app:layout_constraintBottom_toTopOf="@+id/gH2"
        app:layout_constraintEnd_toStartOf="@+id/gV2"
        app:layout_constraintStart_toStartOf="@+id/gV1"
        app:layout_constraintTop_toTopOf="@+id/gH1" />

    <TextView
        android:id="@+id/shapesText"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Shape"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/gH3"
        app:layout_constraintEnd_toStartOf="@+id/gV1"
        app:layout_constraintStart_toStartOf="@+id/guidelineSideLeft"
        app:layout_constraintTop_toTopOf="@+id/gHBreak" />

    <TextView
        android:id="@+id/colourText"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Colour"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/gH4"
        app:layout_constraintEnd_toStartOf="@+id/gV1"
        app:layout_constraintStart_toStartOf="@+id/guidelineSideLeft"
        app:layout_constraintTop_toTopOf="@+id/gH3" />

</android.support.constraint.ConstraintLayout>

和图片: design

以下是观点膨胀的地方:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.filter,null);
dialogBuilder.setView(dialogView);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

这就是我运行时的样子: runtime

我做错了什么?我有

compile 'com.android.support.constraint:constraint-layout:1.1.0'  

根据我的build.gradle文件。

1 个答案:

答案 0 :(得分:0)

由于您的布局没有任何固有高度/宽度,但依赖于其父级通过match_parent高度/宽度的大小。如果没有在通货膨胀中指定父母,则没有可用的大小,因此默认为零。您需要提供父项作为

的第二个参数
View dialogView = inflater.inflate(R.layout.filter,null);

而不是null

我不是100%确定方法应该是什么。这是一个link到Stack Overflow的答案,概述了一种方法。