[Android] Make the android project a jar package and a resource file

Source: Internet
Author: User

Sometimes, we want to provide our android project to third-party developers. At this time, the most common practice is to provide a jar package and a bunch of resource files. Third-party developers can copy the resource files to the corresponding directory of the android project, reference the jar package we provide at the same time, you can use the corresponding API we provide.

Please note that the jar package and resource package are provided to third-party developers.CodeYou cannot reference resources in a way similar to R. layout. Main or R. String. Name.

Therefore, we cannot directly use the r file. Instead, we need to dynamically obtain the resource ID through the field name.

The following encapsulates a class and dynamically obtains the ID using the field name.

Package COM. arui. util; import android. content. context; public class resourceutil {public static int getlayoutid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "layout", paramcontext. getpackagename ();} public static int getstringid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "string", paramcontext. getpackagename ();} public static int getdrawableid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "drawable", paramcontext. getpackagename ();} public static int getstyleid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "style", paramcontext. getpackagename ();} public static int GETID (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "ID", paramcontext. getpackagename ();} public static int getcolorid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "color", paramcontext. getpackagename ());}}

Of course, if you can write all the resource files into the jar package, it is the most convenient for third-party developers. To do this, you must completely process the layout, image resources, and so on in the code, which is relatively troublesome. If there are few elements such as the UI, it is worth trying.

 

 

---------------------------------------------------------------------------

GL(Arui319)

Http://blog.csdn.net/arui319

<This article can be reproduced, but please keep the above author information. Thank you.>

---------------------------------------------------------------------------

 

Related Article

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.