Finding a resource given its ID - as a variable

喜你入骨 提交于 2021-01-28 19:07:05

问题


Finding a resource in your layout is easy if you know the id:

LinearLayout l = FindViewById<LinearLayout>(Resource.Id.linearLayout1);

but say I have

String id = "linearLayout1";

How can I access the resource IDs and "find" the one I'm looking for?


回答1:


You can use this: Resources#getIdentifier()

String id = "linearLayout1";

int resourceId = getResources().getIdentifier(id, "id", getPackageName());

Edit:

I didn't see the Xamarin tag. I am not familiar with Xamarin, but as far as I know, it mimics the Android API, so there should be a similar method available for that.




回答2:


int resID = Resources.GetIdentifier(resourceName, "id", PackageName);

Source



来源:https://stackoverflow.com/questions/19472483/finding-a-resource-given-its-id-as-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!