TIL 52 | [Android] Android Studio의 리소스 참조 : xml -> xml

Yoonsik·2022년 10월 24일

Android

목록 보기
2/3

⛓ Android Studio 에서 Resource 참조 방법

🔗 xml -> xml 참조

프로그램을 생성하는데 사용되는 자원들(color(색깔), string(문자열), dimen(위젯들의 크기 등 .. )은 모두 .xml 파일 형태로 res 디렉터리 아래에 저장됩니다.

또, 화면 레이아웃과 그 안에 텍스트뷰, 버튼 등 과 같이 사용자에게 보이는 것들을 통칭해서 View(뷰) 라고 하며.
이 레이아웃(= 뷰)은 res -> layout -> activity_main.xml이라는 레이아웃 파일에서 만들어집니다.

[안드로이드 뷰 - res 디렉터리의 구조]

  • res
    • layout
      • activity_main.xml
    • colors.xml
    • strings.xml
    • dimens.xml
    • ...

이 activity_main.xml 파일에서 사용자에게 보여지는 화면의 전체적인 레이아웃, 사용할 위젯과 위젯들의 배치를 지정해 주게 되는데,
이 위젯들의 크기나 색깔, 위젯에 사용될 문자열 등 을 res(resoures) 디렉터리 아래에 저장되어 있는 xml 파일 형태의 자원들(colors.xml, strings.xml ...)을 참조하여 activity_main.xml 파일 내의 위젯들에게 적용이 가능합니다.

참조 형식


  • res
    • layout
      • activity_main.xml
    • colors.xml
    • strings.xml
    • dimens.xml
    • ...

색상(color) 자원을 참조하고 싶은 경우 :

형식) @color/colors.xml 파일에저 지정한 name
ex) @color/btn1_color

  • 실제 colors.xml 파일에 들어가보면, 색상 자원들이 다음과 같이 저장되어 있다. (각 자원들의 이름은 개발자 마음대로 지정 가능)
colors.xml

<resources>
	<color name = "White">#FFFFFF</color>
	<color name = "Red">#FF0000</color>
	<color name = "Blue">#0000FF</color>
	<color name = "btn1_color">#D2B48C</color>
	...
</resources>

문자열(string) 자원을 참조하고 싶은 경우 :

형식) @string/strings.xml 파일에저 지정한 name
ex) @string/btn3

  • 실제 strings.xml 파일에 들어가 보면, 문자열 자원들이 다음과 같이 저장되어 있다.
Strings.xml

<resources>
	<string name = "btn1">버튼1</string>
	<string name = "btn2">버튼2</string>
	<string name = "btn3">버튼3</string>
	<string name = "text">환영합니다</string>
  	...
</resources>

위젯들의 크기(dimen) 자원을 참조하고 싶은 경우:

형식) @dimen/dimens.xml 파일에서 지정한 name
ex) @dimen/textSize_medium

  • 실제 dimens.xml 파일에 들어가 보면, 위젯들의 크기 자원들이 다음과 같이 저장되어 있다.
dimes.xml

<resources>
	<dimen name = "textSize_small">10sp</dimen>
	<dimen name="textSize_medium">20sp</dimen>
    <dimen name="textSize_large">30sp</dimen>
    <dimen name="space_small">10dp</dimen>
    <dimen name="space_medium">20dp</dimen>
    <dimen name="space_large">30dp</dimen>
    ...
</resources>

xml -> xml 참조 예시 코드

리소스들을 참조한 레이아웃(activity_main.xml)

activity_main.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"
    android:padding="@dimen/space_large"
    tools:context=".MainActivity2">

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/space_small"
        android:inputType="numberDecimal"
        android:hint="@string/edit_hint"/>

    <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/space_medium"
        android:inputType="numberDecimal"
        android:hint="@string/edit_hint"/>

    <Button
        android:id="@+id/btnPlus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/space_medium"
        android:text="@string/btn_plus"/>
    <Button
        android:id="@+id/btnMinus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/space_medium"
        android:text="@string/btn_minus"/>
    <Button
        android:id="@+id/btnMulti"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/space_medium"
        android:text="@string/btn_multi"/>
    <Button
        android:id="@+id/btnDivide"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/space_medium"
        android:text="@string/btn_divide"/>

    <Button
        android:id="@+id/btnRemainder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/space_medium"
        android:text="@string/btn_remainder"/>

    <TextView
        android:id="@+id/textResult"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:text="@string/text_result"
        android:textSize="@dimen/textSize_large"
        android:textStyle="bold"
        android:textColor="@color/blue"/>


</LinearLayout>

참조된 리소스들(colors.xml, strings.xml, dimens.xml)

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="blue">#FF0000FF</color>
</resources>
dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textSize_small">10sp</dimen>
    <dimen name="textSize_medium">20sp</dimen>
    <dimen name="textSize_large">30sp</dimen>

    <dimen name="space_small">10dp</dimen>
    <dimen name="space_medium">20dp</dimen>
    <dimen name="space_large">30dp</dimen>
</resources>
strings.xml

<resources>
    <string name="app_name">Calculator</string>
    <string name="edit_hint">정수 / 실수 입력</string>
    <string name="btn_plus">더하기</string>
    <string name="btn_minus">빼기</string>
    <string name="btn_multi">곱하기</string>
    <string name="btn_divide">나누기</string>
    <string name="btn_remainder">나머지</string>
    <string name="text_result">계산결과</string>
</resources>
profile
꾸준함의 힘

0개의 댓글