Mobile/Android
[Android]String resource 사용
out of coding
2014. 10. 29. 22:36
안드로이드에서 String Resource 작업시 format사용하는법
String.format 사용시 %s를 사용하게 되는데
이렇게 사용을 하려고 하면, 오류가 발생한다.
Multiple annotations found at this line:
- error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false"
attribute?
- error: Unexpected end tag string
xml에 formatted="false" 를 추가하면 해결이 되지만... 이렇게 쓰는건 결코 좋은 행동이 아니다.
다국어 작업시 두개 이상의 스트링 %s가 포함될 경우 다국어 문맥에 따라 %s의 순서가 변경 될 수 있다.
이때 하드코딩으로 변경하지 말고 안드로이드에서 권고하는 방식을 사용하자.
아래처럼 순서대로 숫자를 붙여주는것. 너무 쉽지만. 귀찮아서 다들 안할거라 생각된다.
<string name="string">Hello, %1$s! You have %2$d new messages.</string>
다음은 구글의 링크.
http://developer.android.com/guide/topics/resources/string-resource.html