ConstraintLayout Treats Multiple Horizontal Chains Differently

后端 未结 3 1684
天命终不由人
天命终不由人 2021-01-24 09:34

I am attempting to set up what I thought should be a simple layout with a ConstraintLayout I have a series of text entries, with labels whose size can change from l

相关标签:
3条回答
  • 2021-01-24 09:50

    My guess would be the fact that you're not really setting up a chain.

    https://developer.android.com/training/constraint-layout/index.html

    A chain works properly only if each end of the chain is constrained to another object on the same axis

    For a proper chain, your labels would need to be part of it as well. You can probably just ditch the chain attributes and constrain the edit texts to the barrier and the parent rights.

    Hope that helps.

    0 讨论(0)
  • 2021-01-24 10:00

    The Android dev team confirmed this is a bug, and it has been fixed in the beta6 release. The layout now works without the need for any chains or spaces (the solution below is what they suggested in the ticket).

    https://issuetracker.google.com/issues/74469361

    <TextView
        android:id="@+id/label_1"
        android:tag="48,103,136,57"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Label 1"
        app:layout_constraintBottom_toBottomOf="@+id/entry_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/entry_1" />
    
    <EditText
        android:id="@+id/entry_1"
        android:tag="505,63,512,136"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginStart="8dp"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_max="200sp"
        app:layout_constraintWidth_min="100sp" />
    
    <TextView
        android:id="@+id/label_2"
        android:tag="48,254,442,57"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Label 2 Is very very long"
        app:layout_constraintBottom_toBottomOf="@+id/entry_2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/entry_2" />
    
    <EditText
        android:id="@+id/entry_2"
        android:tag="505,214,450,136"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginStart="8dp"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/entry_1"
        app:layout_constraintWidth_max="150sp"
        app:layout_constraintWidth_min="100sp" />
    
    <TextView
        android:id="@+id/label_3"
        android:tag="48,405,218,57"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Label Three"
        app:layout_constraintBottom_toBottomOf="@+id/entry_3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/entry_3" />
    
    <EditText
        android:id="@+id/entry_3"
        android:tag="505,365,450,136"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginStart="8dp"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/entry_2"
        app:layout_constraintWidth_max="150sp"
        app:layout_constraintWidth_min="100sp" />
    
    <android.support.constraint.Barrier
        android:id="@+id/guideline"
        android:tag="490,48,0,0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:barrierDirection="right"
        app:constraint_referenced_ids="label_1,label_2,label_3" />
    
    0 讨论(0)
  • 2021-01-24 10:08

    Try it with Relative Layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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"
        android:padding="16dp">
    
        <TextView
            android:id="@+id/label_1"
            android:text="Label 1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
        <EditText
            android:id="@+id/entry_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:layout_alignBaseline="@+id/label_1"
            android:layout_toEndOf="@+id/label_1"
            android:maxLength="20"
            android:textSize="20sp"
            android:inputType="text" />
        <TextView
            android:id="@+id/label_2"
            android:text="Label 2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:textSize="20sp"
            android:layout_below="@+id/label_1"/>
        <EditText
            android:id="@+id/entry_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:layout_alignBaseline="@+id/label_2"
            android:layout_toEndOf="@+id/label_2"
            android:maxLength="20"
            android:textSize="20sp"
            android:inputType="text" />
        <TextView
            android:id="@+id/label_3"
            android:text="Label 3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:textSize="20sp"
            android:layout_below="@+id/label_2"/>
        <MultiAutoCompleteTextView
            android:id="@+id/entry_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:layout_alignBaseline="@+id/label_3"
            android:layout_toEndOf="@+id/label_3"
            android:maxLength="200"
            android:textSize="20sp"/>
    </RelativeLayout>
    

    Edit: This should be your basic structure for form, provided if ConstraintLayout is not the only option.

    • Just use Relative Layout, Table Layout is not necessary here
    • Set value for android:maxLength
    • For including more text-characters use MultiAutoCompleteTextView
    0 讨论(0)
提交回复
热议问题