마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

xml 초보가 질문드립니다..

0 추천
xml의 string.xml 상에서 시간 값을 입력할 때,

 

<string name="time"><xliff:g id="HOUR">%d</xliff:g>:<xliff:g id="MINUTE">%d</xliff:g></string>

 

이렇게 입력 한 뒤,

 

자바 코드 상에서

 

int hour = 1;

int minute = 10;

 

mResource.getString(R.string.time, hour, minute);

 

이런 식으로 입력할 수 있게 되어있던데 xml 상에서 입력받은 숫자값이 1자리면 앞에 0을 붙이게 설정할 순 없나요?

 

xml 설정을

 

<string name="time"><xliff:g id="HOUR">%s</xliff:g>:<xliff:g id="MINUTE">%s</xliff:g></string>

 

이렇게 해놓고

 

자바 코드상에서

 

mResource.getString(R.string.time, ((hour < 10) ? "0" + hour : hour), ....

 

이런 식으로 작성을 해줘도 되긴 하지만 자바에서 설정할 필요 없이 xml 상에서 설정하는 방법은 없는지 궁금하네요.. 혹시 가능하다고 해도 자바에서 처리하도록 만드는거보다 처리 속도가 느려지나요..?
zent (1,360 포인트) 님이 2013년 9월 1일 질문

1개의 답변

+1 추천
 
채택된 답변

XML은 문자열을 삽입할 위치를 지정하는 역할만 하고,

문자열을 형식화 하려면 String.format()을 사용해서 ...

 

http://developer.android.com/intl/ko/guide/topics/resources/string-resource.html#FormattingAndStyling

Formatting strings

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
 
 
0 Pad the number with leading zeros. (Requires width.) format("%07d, %03d", 4, 5555); 0000004, 5555
 

 

 

 

 

Elex (9,090 포인트) 님이 2013년 9월 1일 답변
zent님이 2013년 9월 2일 채택됨
답글 감사드립니다 :)

((hour < 10) ? "0" + hour : hour)이건 xml로 설정 못하는거군요?
...