2015-06-02 41 views

回答

1

您可以通过使用反射在java中完成此操作。

如果你不熟悉它已经,这里是一个很好的起点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: Programatically iterate through Resource ids

0
  1. 进口Field class

    import java.lang.reflect.Field;

  2. 写在你的代码

Field[] ID_Fields = R.drawable.class.getFields(); int[] resourcesArray= new int[ID_Fields.length]; for(int i = 0; i < ID_Fields.length; i++) { try { resourcesArray[i] = ID_Fields[i].getInt(null); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

resourcesArray内容的所有资源文件。

+0

嘿家伙感谢这些我会尽快尝试他们,我会马上回到你身边! –

相关问题