Android
안드로이드 레이아웃 종류 LinearLayout, RelativeLayout, ConstraintLayout
DingCoDing
2022. 10. 1. 03:16
반응형
안드로이드 레이아웃 종류
안드로이드에서 자주 쓰는 레이아웃은 크게 3개가 존재한다.
LinearLayout, RelativeLayout, ConstraintLayout이 있다.
LinearLayout은 horizontal이나 vertical 설정으로 편하게 레이아웃을 구성할 수 있다.
weightSum을 이용하면 웹의 css에서 flex와 같은 역할을 할 수 있다.
RelativeLayout은 뷰 id를 통해서 상대적 위치를 기준으로 뷰의 위치를 정해줄 수 있다.
또한 RelativeLayout을 이용하면 뷰를 겹쳐서 둘 수 있다.
ConstraintLayout은 앵커를 3개 두어 위치를 유동적으로 설정할 수 있다.
1. LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal"
android:weightSum="3"
android:gravity="center_vertical"
>
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#FF0000"
/>
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#FFF200"
/>
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#000DFF"
/>
</LinearLayout>
2. RelativeLayout
<?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"
tools:context=".RelativeLayout">
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FF8F00"
/>
<View
android:id="@+id/blue_view"
android:layout_alignParentLeft="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/design_default_color_primary"
/>
<View
android:id="@+id/green_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FD62FF00"
android:layout_toRightOf="@+id/blue_view"
/>
</RelativeLayout>
3. ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ConstraintLayout">
<View
android:id="@+id/orange_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#F9A825"
/>
<View
android:id="@+id/blue_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/orange_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#1984FF"
/>
<View
android:id="@+id/pink_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FD18F2"
app:layout_constraintTop_toBottomOf="@id/blue_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<View
android:id="@+id/yellow_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFF200"
app:layout_constraintTop_toBottomOf="@id/pink_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<View
android:id="@+id/green_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#27FD18"
app:layout_constraintTop_toBottomOf="@id/yellow_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
반응형