[Android] ConstraintLayout ๐Ÿ“

Jayยท2021๋…„ 3์›” 25์ผ
0

Android

๋ชฉ๋ก ๋ณด๊ธฐ
26/39
post-thumbnail
post-custom-banner

ConstraintLayout

  • ๋ณต์žกํ•œ ๋ ˆ์ด์•„์›ƒ์„ ๋‹จ์ˆœํ•œ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ViewGroup
  • ๋ณต์žกํ•œ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ๋ ˆ์ด ์•„์›ƒ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ์‹์—์„œ ์ž์œ ๋กœ์›Œ์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • ํ˜•์ œ View๋“ค๊ณผ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•ด์„œ ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌ์„ฑํ•œ๋‹ค๋Š” ์ ์ด RelativeLayout๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ, ๋ณด๋‹ค ์œ ์—ฐํ•˜๊ณ  ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

์žฅ์ 

  • ๋ณต์žกํ•œ ๋ ˆ์ด์•„์›ƒ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ๋‹จ์ˆœํžˆ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ž์‹ ๋ทฐ ๊ฐ„์˜ ์ƒํ˜ธ ๊ด€๊ณ„๋ฅผ ์ •์˜ ๊ฐ€๋Šฅํ•˜๋‹ค.

Constraint (์ œ์•ฝ ์กฐ๊ฑด)

  • view์˜ ์œ„์น˜๋ฅผ ์ •์˜ํ•˜๋ ค๋ฉด ๋ณด๊ธฐ์˜ ๊ฐ€๋กœ/์„ธ๋กœ ์ œ์•ฝ ์กฐ๊ฑด์„ ๊ฐ๊ฐ ํ•˜๋‚˜ ์ด์ƒ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค.
  • constraintLayout์—์„œ ์ž์‹ ๋ทฐ์˜ ์œ„์น˜๋ฅผ ์žก๋Š” ๊ธฐ์ค€
  • ๋‹ค๋ฅธ ๋ทฐ๋‚˜ ๋ถ€๋ชจ ๋ ˆ์ด์•„์›ƒ๊ณผ์˜ ์ •๋ ฌ์กฐ๊ฑด/ ์—ฐ๊ฒฐ ๊ด€๊ณ„
  • layout_constraint[๊ธฐ์ค€1]_to[๊ธฐ์ค€2]of = "[viewId || parent]"๋กœ xml text ์ž‘์„ฑ
  • start, end ์†์„ฑ์€ left, right ์†์„ฑ๋ณด๋‹ค ์šฐ์„ ๋จ
  • Top/ Bottom/ Start/ End ๋ฅผ ๋ชจ๋‘ ์„ ์–ธํ•ด์•ผ ํ•˜์ง€ ์•Š์Œ.
  • ์ฒด์ธ ๋“ฑ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ ์‚ฌ์šฉ ์‹œ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋™์ž‘ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ์œผ๋กœ ์™ ๋งŒํ•˜๋ฉด ๋‹ค ์„ ์–ธํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ.

View ํฌ๊ธฐ - ๊ฐ€๋กœ/์„ธ๋กœ ๋น„์œจ ์ง€์ •

  • dimensionRatio : ๋ทฐ์˜ ๊ฐ€๋กœ/์„ธ๋กœ ๋น„์œจ ๊ฒฐ์ •
    • app:layout_constraintDimensionRatio="1" -> ๊ฐ€๋กœ/์„ธ๋กœ 1/1
    • app:layout_constraintDimesionRatio ="1:1" ๊ฐ€๋กœ์„ธ๋กœ 1:1
  • ์ ์–ด๋„ ํ•œ ๋ฐฉํ–ฅ์€ match_constraint ์ด์—ฌ์•ผ ํ•œ๋‹ค.
  • ๋‘ ๋ฐฉํ–ฅ ๋ชจ๋‘ match_constraint์ผ ๊ฒฝ์šฐ, ๋น„์œจ์— ๋งž์ถฐ constraint๋‚ด์—์„œ ๊ฐ€์žฅ ํฐ ํฌ๊ธฐ๋กœ ๊ฒฐ์ •

Guideline

  • android.support.constraint.Guideline
  • ๊ฐ€๋กœ ๋˜๋Š” ์„ธ๋กœ ์ถ• ๋ฐฉํ–ฅ์„ ๊ฐ€์ง„ ๊ฐ€์žฅ์˜ ๋ทฐ
  • ๋ถ€๋ชจ ๋ทฐ์˜ ํŠน์ • ์œ„์น˜๋ฅผ ๊ธฐ์ค€์ ์œผ๋กœ ์‚ผ์„ ๋•Œ ์‚ฌ์šฉ
  • ์ถ•, ์œ„์น˜ ๊ฐ’์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง
    -
    • ์ถ• : andorid : orientation= "[vertical|horizontal]"
    • ์œ„์น˜
      • app:layout_constraintGuide_begin:์‹œ์ž‘์ง€์ ์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ
        • app:layout_constraintGuide_end : ๋ ์ง€์ ์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ
        • app:layout_constraintGuide_percent : ์‹œ์ž‘ ์ง€์ ์œผ๋กœ๋ถ€ํ„ฐ์˜ %์œ„์น˜
<androidx.constraintlayout.widget.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"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/gd_left"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="100dp" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/gd_right"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintGuide_end="100dp" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/gd_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.8" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="hello world"
        app:layout_constraintBottom_toBottomOf="@id/gd_bottom"
        app:layout_constraintEnd_toEndOf="@id/gd_right"
        app:layout_constraintStart_toStartOf="@id/gd_left"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ConstraintSet

  • ํ”„๋กœ๊ทธ๋žจ์ ์œผ๋กœ constraint๋ฅผ ๋งŒ๋“œ๋Š” ๊ธฐ๋Šฅ
    • ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ๋งŒ๋“ค๊ฑฐ๋‚˜
    • ๋‹ค๋ฅธ xml๋กœ๋ถ€ํ„ฐ constraint๋งŒ ๋ฝ‘์•„์˜ค๊ฑฐ๋‚˜
    • ConstraintLayout ์ธ์Šคํ„ด์Šค์—์„œ ๋ฝ‘์•„์˜ค๊ฑฐ๋‚˜ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋งŒ๋“ค์–ด์ง„ constraint๋ฅผ Constraint์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋™์ ์œผ๋กœ ConstraintLayout์˜ ๋ชจ๋“  ๋ทฐ ํ˜น์€ ์ผ๋ถ€ ๋ทฐ์˜ ๋ ˆ์ด์•„์›ƒ์„ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ConstraintSet์€ constraint๋งŒ ๊ฐฑ์‹ ํ•˜๋ฏ€๋กœ constaint์™€ ๊ด€๋ จ์—†๋Š” ์†์„ฑ
  • (padding, text size..)๋Š” ์˜ํ–ฅ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.
  • TransitionManager, beginDelayedTransition()์„ ์ด์šฉํ•ด์„œ ์† ์‰ฝ๊ฒŒ ์• ๋‹ˆ์—์ด์…˜ ์ƒ์„ฑ ๊ฐ€๋Šฅํ•˜๋‹ค.

Reference

profile
developer
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€