Contents

Views 4999 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

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

문자열로 해당 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
No. Category Subject Author Date Views
517 Develop [c] 학생명단 관리 프로그램 소스 ㅋㅋ 1 file hooni 2003.04.23 9080
516 Develop [c#] HTML 이벤트 샘플 소스.. file hooni 2013.04.23 9072
515 Develop [doc] mfc 매뉴얼 다운 받아서 완성하기.. ㅋㄷ file hooni 2013.04.23 9070
514 Develop [linux] tar 명령어 뽀개기.. ㅋㅋ hooni 2003.04.23 9042
513 Develop SVN 초간단 사용하기 hooni 2014.02.28 9010
512 Develop [c++] 레지스트리 편집하는 부분..ㅡㅡ; file hooni 2013.04.23 8997
511 Develop [c++] winsock을 이용한 서버,클라이언트와 ssl서버,클라이언트 file hooni 2013.04.23 8988
510 Develop [c] OpenGL 색 입방체의 회전(입체) hooni 2003.04.23 8973
509 Develop [c++] 윈도우 API 정복 예제 file hooni 2013.04.23 8935
508 Develop [chm] C++ 문법 가이드 file hooni 2013.04.23 8930
507 Develop [c] 시어핀스키 가스킷..(p.582, A.2 - 두번째) hooni 2003.04.23 8920
506 Develop [c] 네트워크 관련 프로그래밍 (포트스캔 탐지 샘플) file hooni 2013.04.23 8917
Board Pagination Prev 1 ... 51 52 53 54 55 56 57 58 59 60 ... 99 Next
/ 99