ConstraintLayout 메모

기본


  • 부모의 왼쪽에 끈


  • <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    기본


  • 부모의 좌우에 붙어 있다
  • 끈이있는 공간의 한가운데에 배치됩니다.


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    layout_constraintHorizontal_bias/layout_constraintVertical_bias


  • 끈이있는 공간에 배치되는 위치를 조정합니다
  • layout_constraintHorizontal_bias가 0으로 왼쪽, 1이 오른쪽으로, 소수로 미세 조정 가능
  • layout_constraintVertical_bias가 0이고, 1이 낮아지고, 소수로 미세 조정 가능


  • <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintHorizontal_bias="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    chain(app:layout_constraintHorizontal_chainStyle="spread")


  • View 도시를 묶어서 체인이 된다
  • 체인이 지정되지 않은 경우 기본적으로 app : layout_constraintHorizontal_chainStyle = "spread"입니다.
  • spread는 여백이 균등하게 배치됩니다.


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/red_view"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <View
            android:id="@+id/red_view"
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:background="#FF0000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/text_view"
            app:layout_constraintRight_toRightOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    app:layout_constraintHorizontal_chainStyle="packed"


  • chain 한 View 도시가 붙어 배치된다


  • <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/red_view"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <View
            android:id="@+id/red_view"
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:background="#FF0000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/text_view"
            app:layout_constraintRight_toRightOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    app:layout_constraintHorizontal_bias


  • 체인 묶음에서 bias가 작동합니다


  • <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/red_view"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <View
            android:id="@+id/red_view"
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:background="#FF0000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/text_view"
            app:layout_constraintRight_toRightOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    app:layout_constrainedWidth



    * text_view의 문장이 길어지면 red_view가 화면 밖으로 튀어 나옵니다.


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!Hello World!HelloHello World!Hello World!HelloHello World!Hello World!Hello"
            android:ellipsize="end"
            android:maxLines="1"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/red_view"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <View
            android:id="@+id/red_view"
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:background="#FF0000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/text_view"
            app:layout_constraintRight_toRightOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • app : layout_constrainedWidth를 true로 지정하면 제약 조건이 적용되고 red_view는 parent에서 벗어나지 않습니다.


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!Hello World!HelloHello World!Hello World!HelloHello World!Hello World!Hello"
            android:ellipsize="end"
            android:maxLines="1"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constrainedWidth="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/red_view"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <View
            android:id="@+id/red_view"
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:background="#FF0000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/text_view"
            app:layout_constraintRight_toRightOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • 좋은 웹페이지 즐겨찾기