프로그램의 모든 이미지를 가져 와서 배열로 푸시하기 위해 리소스 파일을 반복하는 방법이 있는지 궁금합니다.?
그렇다면 가장 간단한 방법은 무엇일까요?
Java에서 Reflection 을 사용하여이를 수행 할 수 있습니다 .
이미 익숙하지 않다면 programmcreek.com 에서 좋은 시작점 을 소개 합니다 .
http://www.programcreek.com/2013/09/java-reflection-tutorial/
간단히 말해,이 샘플 코드를 사용하여 코드에서 R을 반복 할 수 있습니다.
import java.lang.reflect.Field;
import android.util.Log;
public class ResourceUtil {
/**
* Finds the resource ID for the current application's resources.
* @param Rclass Resource class to find resource in.
* Example: R.string.class, R.layout.class, R.drawable.class
* @param name Name of the resource to search for.
* @return The id of the resource or -1 if not found.
*/
public static int getResourceByName(Class<?> Rclass, String name) {
int id = -1;
try {
if (Rclass != null) {
final Field field = Rclass.getField(name);
if (field != null)
id = field.getInt(null);
}
} catch (final Exception e) {
Log.e("GET_RESOURCE_BY_NAME: ", e.toString());
e.printStackTrace();
}
return id;
}
또한 더 많은 통찰력을 얻으 려면이 질문을 참조 할 수 있습니다. Android : 리소스 ID를 프로그래밍 방식으로 반복
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다