ConstraintLayout无法处理某些值

时间:2019-01-04 07:41:39

标签: java android android-studio android-constraintlayout

我正在使用以下布局

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <ImageView
            android:layout_margin="10dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:id="@+id/mainImage"
            android:scaleType="centerInside"
            android:src="@drawable/placeholder"
            />


        <LinearLayout
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            app:layout_constraintLeft_toRightOf="@+id/mainImage"
            app:layout_constraintEnd_toEndOf="parent"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:background="@null"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:inputType="text"
                android:hint="Patient Name"
                android:drawablePadding="10dp"
                android:textSize="14sp"
                android:id="@+id/name"
                />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#80000000"
                />

            <EditText
                android:background="@null"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:inputType="text"
                android:hint="Patient Name"
                android:drawablePadding="10dp"
                android:textSize="14sp"
                android:id="@+id/dob"
                android:visibility="visible"
                />


        </LinearLayout>

        <ListView
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/mainImage"
            android:layout_marginTop="45dp"
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />


    </android.support.constraint.ConstraintLayout>

结果是

enter image description here

患者姓名等应该在图像的右边,列表视图应该在图像视图的底部,为什么它不能识别约束

2 个答案:

答案 0 :(得分:1)

您需要从各个角度约束视图,并在需要占用所有允许空间时使用match_constraint0dp

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

<ImageView
    android:id="@+id/mainImage"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:scaleType="centerInside"
    android:src="@drawable/placeholder"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_marginStart="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="@+id/mainImage"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/mainImage"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toEndOf="@+id/mainImage"
    app:layout_constraintTop_toTopOf="@+id/mainImage">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="@null"
        android:drawablePadding="10dp"
        android:hint="Patient Name"
        android:inputType="text"
        android:textSize="14sp"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#80000000"
        />

    <EditText
        android:id="@+id/dob"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="@null"
        android:drawablePadding="10dp"
        android:hint="Patient Name"
        android:inputType="text"
        android:textSize="14sp"
        android:visibility="visible"
        />


</LinearLayout>

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/mainImage"/>


</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

<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"
    tools:context=".MainActivity">



    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent">

        <ImageView
            android:id="@+id/mainImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_margin="10dp"
            android:scaleType="centerInside"
            android:src="@drawable/ic_launcher_foreground"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:background="@null"
                android:drawablePadding="10dp"
                android:hint="Patient Name"
                android:inputType="text"
                android:textSize="14sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#80000000" />

            <EditText
                android:id="@+id/dob"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:background="@null"
                android:drawablePadding="10dp"
                android:hint="Patient Name"
                android:inputType="text"
                android:textSize="14sp"
                android:visibility="visible" />
        </LinearLayout>


    </LinearLayout>


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="406dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />


</android.support.constraint.ConstraintLayout>

我刚刚在我的项目上测试了此代码,此方法奏效了。只需根据您的项目更改一些变量即可。谢谢! 这是您将从此代码中得到的:

enter image description here