본문 바로가기
안드로이드 자바

[Android][Java] 뷰(View)의 크기 및 간격 공통 관리 하기

by teamnova 2023. 3. 15.

안녕하세요.

오늘은 안드로이드 레이아웃에서 뷰의 크기와 간격 등을 공통적으로 관리하는 방법에 대해 알아보겠습니다.

바로 공통 관리 파일을 만들어 그 안에 크기와 간격의 설정값을 담아두고 사용하는 것인데요.

먼저 공통 관리 파일을 생성합니다.

 

res -> values -> 마우스 오른쪽 -> new -> Values Resource File

파일 이름 : dimen

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="width">100dp</dimen>
    <dimen name="height">200dp</dimen>
    <dimen name="margin_top">50dp</dimen>
    <dimen name="margin_left">15dp</dimen>
    <dimen name="margin_right">15dp</dimen>
    <dimen name="margin_bottom">40dp</dimen>
</resources>

 width: 가로 크기 값

 height: 세로 크기 값

 margin_top: 위와 간격 

 margin_left: 왼쪽과의 간격

 margin_right: 오른쪽과 간격

 margin_bottom: 아래와 간격

 

다음으로 해당 리소스 파일 설정 값을 사용기 위한 xml 파일을 만들어줍니다.

 

activity_dimen.xml

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/margin_top"
        android:layout_marginBottom="@dimen/margin_bottom"
        android:background="#FF03DAC5"
        android:gravity="center"
        android:text="TextView"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="@dimen/width"
            android:layout_height="@dimen/height"
            android:layout_marginStart="@dimen/margin_left"
            android:layout_marginTop="@dimen/margin_top"
            android:layout_marginEnd="@dimen/margin_right"
            android:background="#FFBB86FC"
            android:gravity="center"
            android:text="Dimen1"
            android:textSize="30sp" />

        <TextView
            android:layout_width="@dimen/width"
            android:layout_height="@dimen/height"
            android:layout_marginTop="@dimen/margin_top"
            android:layout_marginEnd="@dimen/margin_right"
            android:background="#FFBB86FC"
            android:gravity="center"
            android:text="Dimen2"
            android:textSize="30sp" />

        <TextView
            android:layout_width="@dimen/width"
            android:layout_height="@dimen/height"
            android:layout_marginStart="@dimen/margin_left"
            android:layout_marginTop="@dimen/margin_top"
            android:background="#FFBB86FC"
            android:gravity="center"
            android:text="Dimen3"
            android:textSize="30sp" />
    </LinearLayout>


</LinearLayout>

사용법 : @dimen/dimen name

 

하나 이상의 뷰 크기 및 간격이 같은 상황에서 하나 하나 값을 정했을 때 수정해야 하는 상황이 생긴다면 번거로워집니다.

하지만 이때 공통 파일에 값을 넣어놓고 해당 공통 파일을 활용했다면 공통 파일의 값만 수정하면 되는거죠.

 

마지막으로 실행화면 입니다.

오늘은 이렇게 공통 관리 파일을 통해 뷰의 값은 크기 혹은 간격 값을 쉽게 관리하는 방법에 대해 알아보았습니다.

궁금한 점은 언제든지 댓글로 남겨주세요.

감사합니다.