Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

Dynamic Resource Loading Android

I'm trying to find a way to open resources whose name is determined at runtime only.

More specifically, I want to have a XML that references a bunch of other XML files in the application apk. For the purpose of explaining, let's say the main XML is main.xml and the other XML are file1.xml, file2.xml and fileX.xml. What I want is to read main.xml, extract the name of the XML I want (fileX.xml), for example, and then read fileX.xml. The problem I face is that what I extract form main.xml is a string and I can't find a way to change that to R.raw.nameOfTheFile.

Anybody has an idea?

I don't want to:

  • regroup everything in one huge XML file
  • hardcode main.xml in a huge switch case that links a number/string to the resource ID
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I haven't used it with raw files or xml layout files, but for drawables I use this:

getResources().getIdentifier("fileX", "drawable","com.yourapppackage.www");

to get the identifier (R.id) of the resource. You would need to replace drawable with something else, maybe raw or layout (untested).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...