Contents

조회 수 4081 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

동적인 문자열로 리소스 가져오는 방법.

문자열로 해당 Resource (혹은 레이아웃에 포함된 View) 의 ID 값을 가져온다;


-----------------------------------------------------------------

getResources().getIdentifier(패키지명:디렉토리/파일명, null, null);

또는,

getResources().getIdentifier(파일명, 디렉토리명, 패키지명);

-----------------------------------------------------------------


String resName = "@drawable/imgEnd";
String packName = this.getPackageName(); // 패키지명
int resID = getResources().getIdentifier(resName, "drawable", packName);

String viewName = "imgViewEnd";
String packName = this.getPackageName(); // 패키지명
int resID = getResources().getIdentifier(resName, "id", packName);


주로 리소스/뷰의 이름을 조합형식으로 만들어 가져와야 할 경우 이런 코드를 사용할 수 있다.

예를 들어,

for (int i=0; i < 3; i++) {
    String resName = "@drawable/img_" + i;
    // 결론적으로 이미지 리소스 이름은 img_1, img_2, img_3 이 되겠다;
    int resID = getResources().getIdentifier(resName, "drawable", packName);

    ImageView iv = (ImageView)findViewById(R.id.imgView);
    iv.setImageResource(resID); // 이미지뷰의 이미지를 설정한다;
}


적용한 샘플코드 ㅋㄷ

int count = 0;
for(int r=0; r<puzButtons.length; r++){
    for(int c=0; c<puzButtons[r].length; c++){
    // getResources().getIdentifier(패키지명:디렉토리/파일명, null, null);
    //String buttonName = getPackageName() + ":id/btn" + r + c;
    //int id = getResources().getIdentifier(buttonName, null, null);

    // 또는, getResources().getIdentifier(파일명, 디렉토리명, 패키지명);
    String idName = "btn" + r + c;
    int id = getResources().getIdentifier(idName, "id", getPackageName());

    puzButtons[r][c] = (Button)findViewById(id);
    puzButtons[r][c].setText(Integer.toString(ar[count]));
    if(listen == true) {
        puzButtons[r][c].setOnClickListener(listener);
    }
    puzButtons[r][c].setTag(new Point(r,c));

    if(ar[count] == 0){
        curPoint.set(r, c);
        puzButtons[r][c].setText("");
    }

    count++;
    }
}


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
441 Develop [js] 네이버에서 그림 퍼올 때.. URL Encoding 관련ㅋㅋ hooni 2013.04.23 7252
440 Develop [jsp] 초간단한 include 구문 예제.. file hooni 2013.04.23 7246
439 Develop [c] 문자열 라이브러리 최신버전 file hooni 2003.04.23 7223
438 Develop [c] 네트워크 관련 프로그래밍 (포트스캔 탐지 샘플) file hooni 2013.04.23 7221
437 Develop [c] 함수 목록과 예제 소스 파일 file hooni 2013.04.23 7211
436 Develop [c] 시스템 보안 과제.. 시간(amc time) 변경 file hooni 2013.04.23 7207
435 Develop [c++] template 사용예 hooni 2003.04.23 7207
434 Develop [php] 탐색기와 같은 다이나믹 트리(xml/xsl 이용) file hooni 2013.04.23 7206
433 Develop [c] 시스템공학 레포트 (pass1, pass2) file hooni 2003.04.23 7206
432 Develop [c] 가변인자 함수(printf와 같은..) hooni 2013.04.23 7204
431 Develop [c++] 트리컨트롤 예제1 ㅋㅋ file hooni 2013.04.23 7197
430 Develop [c] 연산자 우선순위.. ㅋㅋ hooni 2013.04.23 7196
Board Pagination Prev 1 ... 57 58 59 60 61 62 63 64 65 66 ... 98 Next
/ 98