Contents

Views 4081 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
513 Develop [php] 자바스크립트 개판 만들기.. file hooni 2013.04.23 7688
512 Develop SVN 초간단 사용하기 hooni 2014.02.28 7684
511 Develop [c#] BFilter 툴바 소스 코드 ㅎㅎ file hooni 2013.04.23 7672
510 Develop [java] 파일 라인수 계산하는 프로그램 (하위 디렉토리까지..) file hooni 2013.04.23 7667
509 Develop [js] 핫키(단축키) 구현방법 hooni 2003.04.23 7656
508 Develop [c#]업글 뉴 툴바 개인적으로 만든거.. (new) ㅋㅋ secret hooni 2013.04.23 7651
507 Develop [c] OpenGL 관측점 이동 hooni 2003.04.23 7646
506 Develop [js] 자바스크립트와 정규표현식 메뉴얼 (chm 형식) file hooni 2013.04.23 7645
505 System/OS HTTPS와 SSL 인증서 file hooni 2014.03.11 7643
504 Develop [c++] winsock을 이용한 서버,클라이언트와 ssl서버,클라이언트 file hooni 2013.04.23 7643
503 Develop [js] 비만 지수 측정(BMI) ㅎㅎ file hooni 2013.04.23 7639
502 Develop [c++] 초간단 스택 두 가지 방식(class, struct) file hooni 2013.04.23 7638
Board Pagination Prev 1 ... 51 52 53 54 55 56 57 58 59 60 ... 98 Next
/ 98