상세 컨텐츠

본문 제목

앱 개발 방법 - 앱 리소스 (코드 내 리소스 액세스 [코드로 리소스를 참조하는 데 쓰는 구문, 사용사례])

안드로이드 프로그래밍/앱 리소스

by green post it 2022. 1. 7. 15:46

본문

반응형

코드 내 리소스 액세스

리소스 ID를 메서드 매개변수로 전달하면 코드 내 리소스를 사용할 수 있습니다. 예를 들어 ImageView를 설정하여 setImageResource()를 사용하는 res/drawable/myimage.png 리소스를 사용할 수 있습니다.

Java

val imageView = findViewById(R.id.myimageview) as ImageView

imageView.setImageResource(R.drawable.myimage)

Kotlin

val imageView = findViewById(R.id.myimageview) as ImageView

imageView.setImageResource(R.drawable.myimage)

 

Resources에서 메서드를 사용하는 개별 리소스를 검색할 수도 있으며 이는 getResources()로 인스턴스를 가져올 수 있습니다.

구문

다음은 코드로 리소스를 참조하는 데 쓰는 구문입니다.

 

[<package_name>.]R.<resource_type>.<resource_name>

 

  • <package_name>는 리소스가 위치한 패키지의 이름입니다.(자체 패키지의 리소스를 참조할 경우에는 필요하지 않음)
  • <resource_type>는 해당 리소스 유형의 R 하위 클래스입니다.
  • <resource_name>는 확장자가 없는 리소스 파일 이름이거나 XML 요소의 android:name 특성 값입니다.(단순 값의 경우)

사용사례

리소스 ID 매개변수를 허용하는 메서드가 많이 있고 Resources에서 메서드를 사용하여 리소스를 검색할 수 있습니다. Context.getResouces()로 Resources의 인스턴스를 가져올 수 있습니다.

 

다음은 코드로 리소스에 액세스한 몇 가지 예시입니다.

Java

// Load a background for the current screen from a drawable resource
//드로어블 리소스에서 현재 화면의 배경 로드
getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ;

// Set the Activity title by getting a string from the Resources object, because
//리소스 개체에서 문자열을 가져와서 액티비티 제목을 설정합니다.
//  this method requires a CharSequence rather than a resource ID
//이 메서드에는 리소스 ID가 아닌 문자 시퀀스(CharSequence)가 필요합니다.
getWindow().setTitle(getResources().getText(R.string.main_title));

// Load a custom layout for the current screen
//현재 화면에 대한 사용자 정의 레이아우 로드
setContentView(R.layout.main_screen);

// Set a slide in animation by getting an Animation from the Resources object
//리소스 개체에서 애니메이션을 가져와 애니메이션에서 슬라이드 설정
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
        R.anim.hyperspace_in));

// Set the text on a TextView object using a resource ID
//리소스 ID를 사용하여 TextView 개체의 텍스트를 설정합니다.
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello_message);

Kotlin

// Load a background for the current screen from a drawable resource
//드로어블 리소스에서 현재 화면의 배경을 로드합니다.
window.setBackgroundDrawableResource(R.drawable.my_background_image)

// Set the Activity title by getting a string from the Resources object, because
//Resources 객체에서 문자열을 가져와서 Activity 제목을 설정합니다.
//  this method requires a CharSequence rather than a resource ID
//이 메서드에는 리소스 ID가 아닌 CharSequence가 필요합니다.
window.setTitle(resources.getText(R.string.main_title))

// Load a custom layout for the current screen
//현재 화면에 대한 사용자 정의 레이아웃 로드
setContentView(R.layout.main_screen)

// Set a slide in animation by getting an Animation from the Resources object
//리소스 개체에서 애니메이션을 가져와 애니메이션에서 슬라이드 설정
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
        R.anim.hyperspace_in))

// Set the text on a TextView object using a resource ID
//리소스 ID를 사용하여 TextView 개체의 텍스트를 설정합니다.
val msgTextView = findViewById(R.id.msg) as TextView
msgTextView.setText(R.string.hello_message)

 

주의 : 수동으로 R.java 파일을 수정해서는 안 됩니다. 프로젝트가 컴파일되었을 때 aapt 도구가 생성한 파일이기 때문입니다. 모든 변경 사항은 다음번 컴파일에서 재정의됩니다.

 

리소스 ID를 메서드 매개변수로 전달하면 코드 내 리소스를 사용할 수 있다. 예를 들어 ImageView를 설정하여 setImageResource()를 사용하는 res/drawable/myimage.png 리소스를 사용할 수 있다.

(예제)
Java
ImageView imageView = (ImageView) findViewById(R.id.myimageview);imageView.setImageResource(R.drawable.myimage);

Kotlin
val imageView = findViewById(R.id.myimageview) as ImageView
imageView.setImageResource(R.drawable.myimage)

(리소스 액세스 예제)
//드로어블 리소스에서 현재 화면의 배경 로드 getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ;

//리소스 개체에서 문자열을 가져와서 액티비티 제목을 설정합니다.
//이 메서드에는 리소스 ID가 아닌 문자 시퀀스(CharSequence)가 필요합니다. getWindow().setTitle(getResources().getText(R.string.main_title));

//현재 화면에 대한 사용자 정의 레이아우 로드
setContentView(R.layout.main_screen);

//리소스 개체에서 애니메이션을 가져와 애니메이션에서 슬라이드 설정 flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.hyperspace_in));

//리소스 ID를 사용하여 TextView 개체의 텍스트를 설정합니다.
TextView msgTextView = (TextView) findViewById(R.id.msg); msgTextView.setText(R.string.hello_message);
반응형

관련글 더보기