본문 바로가기

안드로이드 코틀린

ListView 밑에 Button/TextView 넣기 (Layout의 나머지 공간차지)

반응형

흔한 이메일 작성창

위와 같은 배치에서 To/Subject와 Send는 정해진 Height를 가졌고, Message 부분이 그 나머지 공간을 차지하게 되는데, 이런 경우 LinearLayout에서 layout_weight 속성을 이용하면 된다. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/send" />
</LinearLayout>

Message에만 layout_weight 속성을 1을 주고 나머지는 아무 값도 주지 않으면 위와 같은 배열이 된다.

마찬가지로, ListView나 RycyclerView를 사용할 때, 그 밑에 다른 뷰를 추가 하고 싶으면 위와 같이 layout_weight를 1로 주면 된다.

리스트뷰가 화면을 채우고, 그 밑에 다른 뷰가 자리잡음

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:dividerHeight="1dp"
        android:divider="@color/colorPrimaryDark"
        />


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#2196F3"
        android:gravity="center"
        android:text="여기에는 텍스트 뷰가 있습니다."
        android:textColor="#FFFFFF" />

    
</LinearLayout>

물론, ListView나 RecyclerView에는 각자 헤더와 푸터를 추가하는 기능이 따로 있으므로, 더 유용한 방법으로 구현하는 것이 적절함.

반응형