安卓:通过资源ID编程方式遍历遍历、方式、资源、ID

由网友(为迩独战天下さ)分享简介:我希望能够遍历所有生成的R档的字段。是这样的:的(INT ID:R.id.getAllFields()){//做一些与身份证一样,为每个图像的图}我尝试过反思,但我似乎无法加载所包含的R类内特定的内部类。因此,举例来说,这不会为我工作:C类=的Class.forName(packageName.R.id)我可以反映...

我希望能够遍历所有生成的R档的字段。

是这样的:

 的(INT ID:R.id.getAllFields()){
//做一些与身份证一样,为每个图像的图
}
 

我尝试过反思,但我似乎无法加载所包含的R类内特定的内部类。因此,举例来说,这不会为我工作:

  C类=的Class.forName(packageName.R.id)
 

我可以反映R类本身,而是我需要的ID类中的字段。

我也试过翻翻资源类,但找不到任何东西在那里。在这种情况下,看来你可以采取RESOURCEID并获得该ID的字符串名称,或者拿一个字符串名称,并得到相应的RESOURCEID。我找不到像什么:

  INT [] Resources.getAllResourceIDs()
 
Android 资源管理框架 Resources和AssetManager 及 资源编译 aapt

也许我会对此错误的。或者,也许我不应该打的手打字他们,例如:

  INT [] myIds = {R.id.firstResource,R.id.secondResource}
 

该方法与我的UI设计工作时,不是那么灵活的缺点。每当他增加了一个新的资源到XML文件,我将不得不更新code。显然不是太痛苦了,但它仍然是不错的,它似乎像它应该是可行的。

编辑:

低于ViewGroup.getChildCount()答案/ ViewGroup.getChildAt()工作正常。但是,我也必须找到一个方法来实例化我的XML的ViewGroup /布局。要做到这一点,你可以试试:

  LayoutInflater李= MyActivity.getLayoutInflater();
ViewGroup中VG =(ViewGroup中)li.inflate(R.layout.main,NULL);
 

解决方案

您对我的评论的回复帮助我更好地了解你正在尝试做的。

您也许可以使用 的ViewGroup#getChildAt 和的 的ViewGroup#getChildCount 通过各种的ViewGroup 的弧圈在你看来等级和执行的instanceof 在返回查看旅行支票。然后,你可以做任何你想做取决于孩子意见的类型和他们在您的层次结构。

I want to be able to iterate through all of the fields in the generated R file.

Something like:

for(int id : R.id.getAllFields()){
//Do something with id, like create a view for each image
}

I've tried reflection, but I can't seem to load a specific inner class that's contained inside the R class. So, for example, this wouldn't work for me:

Class c = Class.forName("packageName.R.id")

I can reflect on the R class itself, but I need the fields within the id class.

I also tried looking through the Resources class, but couldn't find anything there. In that case, it seems you can take a resourceID and get the string name of that id, or take a string name and get the corresponding resourceID. I couldn't find anything like:

int[] Resources.getAllResourceIDs()

Maybe I'm going about this wrong. Or maybe I shouldn't fight typing them all in by hand, e.g.:

int[] myIds = {R.id.firstResource, R.id.secondResource}

This approach has the downside of not being as flexible when working with my UI designer. Whenever he adds a new resource to the XML file, I'll have to update the code. Obviously not too painful, but it would still be nice to have and it seems like it should be doable.

EDIT:

The answer below about ViewGroup.getChildCount()/ViewGroup.getChildAt() works fine. But, I also had to find a way to instantiate my XML ViewGroup/Layout. To do that, try something like:

LayoutInflater li = MyActivity.getLayoutInflater();
ViewGroup vg = (ViewGroup) li.inflate(R.layout.main, null);

解决方案

Your reply to my comment helped me get a better idea of what you're trying to do.

You can probably use ViewGroup#getChildAt and ViewGroup#getChildCount to loop through various ViewGroups in your view hierarchy and perform instanceof checks on the returned Views. Then you can do whatever you want depending on the type of the child views and where they are in your hierarchy.

阅读全文

相关推荐

最新文章