본문 바로가기

TLI/Android

ConstraintLayout의 weight 비율 조정

개요

  • ConstraintLayout내에서 같은 레벨인 View들은 화면의 비율을 나눠 가질 수 있다.
  • vertical로 한다면 모두 height=0 / horizontal로 한다면 width=0으로 해줘야 한다.
  • vertical / horizontal에 따라 각각의 view에 `app:layout_constraintVertical/Horizontal_weight`의 값을 비율로 나눠 주면된다.
<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layoutHeader"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        app:layout_constraintVertical_weight="0.1"
        app:layout_constraintBottom_toTopOf="@+id/postRecyclerView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/btnPostAddress"
            android:layout_width="124dp"
            android:layout_height="60dp"
            android:gravity="right|center"
            android:background="#00000000"
            android:text="내배캠동"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:layout_width="30dp"
            android:layout_height="30dp"
            app:layout_constraintStart_toEndOf="@+id/btnPostAddress"
            app:layout_constraintTop_toTopOf="@id/btnPostAddress"
            app:layout_constraintBottom_toBottomOf="@id/btnPostAddress"
            android:background="@drawable/ic_main_open_address"

            />

        <Button
            android:id="@+id/btnPostNotification"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:gravity="center_vertical"
            android:background="@drawable/ic_notification"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/postRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintVertical_weight="0.9"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layoutHeader">
    </androidx.recyclerview.widget.RecyclerView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/btnMainFloating"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_margin="40dp"
        android:src="@drawable/ic_main_to_top"
        android:visibility="invisible"
        app:shapeAppearance="?attr/cornerFamily"
        app:fabCustomSize="75dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

'TLI > Android' 카테고리의 다른 글

border line 만들기  (0) 2024.08.28
OnBackPressedCallback(true)  (0) 2024.08.25
ListAdapter의 holder 재사용  (0) 2024.08.24
Retrofit  (0) 2024.08.09
Shared Preference / Room  (0) 2024.08.06