Android code loads a resource file with a known file name

Source: Internet
Author: User

If you need a resource in Android development, use the r.drawable.** file directly. However, if at first you do not know which resource file to load, you must obtain the ID value dynamically in the program and load it. The loading process is loaded using the file name, as follows:

public static String Resource_path = "android.resource://PackageName/";
Suppose there is a resource picture named Content_icon
Public Uri Geticonuri (String drawablename) {
if (Drawablename = = null) {
return uri.empty;
}
Return Uri.parse (Drawablename);
}


Flag.png, for example, can be used:
int idflag = Getresources (). Getidentifier (Getpackagename () + ":d Rawable/flag", NULL, NULL);
Or
int idflag = Getresources (). Getidentifier ("Flag", "drawable", Getpackagename ());
int idflag = Getresources (). Getidentifier (Getpackagename () + ":d Rawable/flag", NULL, NULL);
Or
int idflag = Getresources (). Getidentifier ("Flag", "drawable", Getpackagename ());

Here, how to get Flag1.png ~ Flag10.png drawable Objects, the code is as follows:
var drawable[] dw = new DRAWABLE[10];

for (int i = 1; i <=; i++) {
int id = getresources (). Getidentifier ("flag" + I, "drawable", Getpackagename ());
Dw[i-1] = Getresources (). getdrawable (ID);
}

Android code loads a resource file with a known file name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.