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.