안드로이드
안드로이드 문자열 리소스(strings.xml) 사용 예제
devGeek
2021. 10. 19. 00:05
반응형
문자열 리소스(strings.xml) - /res/values/strings.xml
안드로이드 프로젝트에서 /res/value 경로 아래에 strings.xml이라는 문자열 리소스 파일이 있다.
사용할 문자열 추가하기
프로젝트를 처음 생성하면 아래와 같이 기본으로 app_name이라는 이름을 가진 문자열 리소스가 있다. app_name과 같은 형태로
<resources>
<string name="app_name">DataBindingBasics</string>
<string name="name">devGeek</string>
<string name="what_is_your_nickname">What is your nickname?</string>
</resources>
소스에서 문자열 리소스 사용하기 - 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:paddingHorizontal="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/name"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/what_is_your_nickname"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
<TextView> 태그의 속성 중 android:text=""에 <string> 태그의 name 속성 값을 넣어준다.
ex). android:text="@string/name"
ex). android:text="@string/what_is_your_nickname
결과 화면
반응형