对于每个字段,约束布局必须是单独的,否则它将是一个父布局

时间:2018-05-31 09:01:05

标签: android android-layout android-constraintlayout android-relativelayout

我是第一次处理约束布局。我有点困惑的事实是,有必要让屏幕上的每个组件都有自己的约束布局,以便它能够响应,或者我可以在父约束布局上创建并将所有组件放在内部,它将表现为响应?

我的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"
    tools:context=".LandingPage"
    tools:layout_editor_absoluteY="25dp">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="24dp"
        android:layout_marginEnd="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginRight="22dp"
        android:layout_marginStart="22dp"
        android:layout_marginTop="179dp"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <EditText
            android:id="@+id/editText14"
            android:layout_width="0dp"
            android:layout_height="58dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/edit_text_background"
            android:ems="10"
            android:inputType="phone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="7dp"
            android:background="#ffffff"
            android:text="Label"
            app:layout_constraintBottom_toTopOf="@+id/editText14"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/editText14" />
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="30dp"
        android:layout_marginEnd="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginRight="22dp"
        android:layout_marginStart="22dp"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout">

        <EditText
            android:id="@+id/editText12"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/edit_text_background"
            android:ems="10"
            android:inputType="textPersonName"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="58dp"
        android:layout_marginEnd="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginRight="22dp"
        android:layout_marginStart="22dp"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout2">

        <EditText
            android:id="@+id/editText13"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/edit_text_background"
            android:ems="10"
            android:inputType="textPersonName"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="66dp"
        android:layout_marginEnd="29dp"
        android:layout_marginLeft="29dp"
        android:layout_marginRight="29dp"
        android:layout_marginStart="29dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout3">

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/signup_button_background"
            android:text="@string/signupButton"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:1)

我不太清楚你的问题很清楚但是约束布局的主要目的是让一个父(约束布局)和其中的所有视图,所以你只有一个层次的层次结构。

我有一个例子:

<?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:id="@+id/hubConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    tools:ignore="MissingPrefix">

    <ImageView
        android:id="@+id/hubColorImageView"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:contentDescription="@null"
        app:layout_constraintBottom_toBottomOf="@+id/percentageTextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/titleTextView"
        app:srcCompat="@drawable/bg_circle"
        tools:tint="@color/blue" />

    <TextView
        android:id="@+id/titleTextView"
        fontPath="@string/fonts_default_regular"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="16dp"
        android:textAllCaps="true"
        app:layout_constraintEnd_toStartOf="@+id/sumTextView"
        app:layout_constraintStart_toEndOf="@+id/hubColorImageView"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="cool title" />

    <TextView
        android:id="@+id/sumTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="0dp"
        android:paddingRight="8dp"
        android:textColor="#636363"
        android:textSize="15sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/titleTextView"
        tools:text="54 000,00€" />

    <TextView
        android:id="@+id/percentageTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:textColor="@color/magnesium"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/titleTextView"
        app:layout_constraintTop_toTopOf="@+id/performanceTextView"
        tools:text="100,0% of contract" />

    <TextView
        android:id="@+id/performanceTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="end"
        android:text=""
        android:textColor="@color/magnesium"
        android:textSize="12sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/percentageTextView"
        app:layout_constraintTop_toBottomOf="@+id/titleTextView"
        tools:text="10000,00 €" />

    <TextView
        android:id="@+id/permanentTransferInfoTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/hubColorImageView"
        app:layout_constraintTop_toBottomOf="@+id/percentageTextView"
        tools:text="you have lot of money" />

    <ProgressBar
        android:id="@+id/customBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:foreground="@android:color/transparent"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/permanentTransferInfoTextView"
        app:layout_constraintTop_toBottomOf="@+id/permanentTransferInfoTextView"
        tools:visibility="visible" />

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

所有具有约束布局作为其直接父级的布局将支持所有约束并且响应性地工作但是如果要对其他ViewsGroup(例如CardView)内的视图具有约束,则还必须在其中指定ConstraintLayout。对于此类实现,请尝试此link