Android jar Resource

Source: Internet
Author: User

The resource file referenced by jar must be placed in the assets folder, and the layout file and drawable must be in the compiled format. Otherwise, the layout file cannot be parsed and the general drawable size is incorrect, nine-patch cannot be stretched.

The layout file extraction method is as follows, where filename must contain assets/path:

Public static view extractview (context, string filename, viewgroup root) throws exception {
Xmlresourceparser parser = context. getassets (). openxmlresourceparser (filename );
Layoutinflater Inflater = (layoutinflater) Context. getsystemservice (context. layout_inflater_service );
Return Inflater. Inflate (parser, root );

}

Note: If the root is null, the root element attributes of the layout file will be ignored, such as fixed length and width. One solution is to nest a viewgroup in the outer layer.

The method for extracting drawable is as follows, where filename does not need to include assets/path:

Public static drawable extractdrawable (context, string filename) throws exception {

Inputstream = context. getassets (). Open (filename );
Typedvalue value = new typedvalue ();

/** Input typevalue. The pixel density of the specified resource is based on the 320*480 screen */
Value. density = 160;

/** Input resources to obtain the pixel density of the target screen */

Drawable = drawable. createfromresourcestream (context. getresources (), value, inputstream, filename );
Inputstream. Close ();
Return drawable;
}

When statelistdrawable is added, the Enabled state must be placed at the end. If the value is false, you only need to take the opposite number of State constants.

If progress is declared in the XML fileCodeChanging progressdrawable is invalid. You must remove the preceding statement.

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.