1. Related folders in the Android project folder, the main resource files are placed in the Res folder. The assets folder is a native file that is not compiled and processed, that is, the files inside the folder will not be precompiled like Xml,java files, and can store some pictures, Html,js, CSS, and other files. We'll show you how to read the resources of the assets folder later! Several folders inside the Res folder are introduced (the Chinese version of the Android Development Guide from the Web):
Catalogue Directory |
Resource Type Resource Types |
res/anim/ |
XML files, which are compiled into frames by frame animation (frame by frame animation) or tweened animation (tweened animation) objects |
res/drawable/ |
. png,. 9.png,. jpg files, which are compiled into the following drawable resource subtypes: To get a resource of this type, you can use the resource.getdrawable (ID) Bitmap file 9-patches (variable-size bitmap) In order to obtain the resource type, use Mcontext.getresources (). getdrawable (R.drawable.imageid) Note: The image resources placed here may be automatically optimized for lossless compression by the AAPT tool. For example, a true color but does not require a 256-color PNG may be converted to a 8-bit PNG with a palette. This allows images of the same quality to occupy less resources. So we have to realize that these binary images placed in this directory may change at the time of generation. If you want to read an image bit stream and convert it to a bitmap (bitmap), place the image file in the res/raw/directory so that you can avoid being automatically optimized. |
res/layout/ |
An XML file that is compiled into a screen layout (or part of a screen). See layout declarations (declaring layouts) |
| res/values/ |
can be compiled into XML files of many types of resources. Note : Unlike other res/folders, it can hold any number of files that hold the description of the resource to be created, not the resource itself. The XML element type controls where these resources should be placed in the R class. Although the files in this folder can be arbitrarily named, the following are some of the more typical files (the Convention for file naming is to include element types in the name): Array.xml Define an array colors.xml A string value that defines the color drawable and colors (color string values). Use Resource.getdrawable () and Resources.getcolor () to obtain these resources separately. dimens.xml defines the dimension value (dimension value). Use Resources.getdimension () to obtain these resources. strings.xml defines a string (string) value. Use Resources.getstring () or Resources.gettext () to obtain these resources. GetText () retains a rich text style applied on the UI string. styles.xml Define style object. |
res/xml/ |
Arbitrary XML files can be read at run time by calling Resources.getxml (). |
res/raw/ |
Copy directly to any file in the device. They do not need to be compiled and added to the compressed file generated by your application compilation. To use these resources, you can call Resources.openrawresource (), which is the ID of the resource, which is r.raw. somefilename. |
2. The automatically generated R class has a R.java in the Gen folder of the project folder, and the resources we normally refer to refer to variables of this class. Note: The R class is automatically generated, and it cannot be modified manually. When a resource changes, it is automatically modified. 3. Using resources in your code the following is a syntax for referencing resources: R.resource_type.resource_name or Android. R.resource_type.resource_name where Resource_type is a subclass of R that holds a specific type of resource. Resource_name is a resource that is defined in an XML file.
Name Property, or other file types are resource-defined file names (not including extensions, which refer to icon.png-like files within the Drawable folder, Name=icon).
AndroidContains a number of standard resources, such as screen styles and button backgrounds. To reference these resources in your code, you must use Android to qualify them, as
Android. R.drawable.button_background。 Here are some examples of the correct and incorrect uses of compiled resources in code:
- Load a background for the current screens from a drawable resource.
- This.getwindow (). Setbackgrounddrawableresource (R.drawable.my_background_image);
- wrong sending a string resource reference into a
- Method that expects a string.
- This.getwindow (). Settitle (R.string.main_title);
- right need to get the title from the Resources wrapper.
- This.getwindow (). Settitle (Resources.gettext (r.string.main_title));
- Load a custom layout for the current screen.
- Setcontentview (R.layout.main_screen);
- Set a slide in animation for a Viewflipper object.
- Mflipper.setinanimation (Animationutils.loadanimation (This,
- r.anim.hyperspace_in));
- Set the text on a TextView object.
- TextView Msgtextview = (TextView) Findviewbyid (r.id.msg);
- Msgtextview.settext (R.string.hello_message);
Check the SDK Doc, just understand why Window.settitle first Resources.gettext, the original settitle parameter is charsequence,resources.gettext (int) The return is charsequence, while the other settext parameters are charsequence and some are int (this is the resources variable value). At the same time, officials also gave two examples of using system resources:
- Display icons for standard applications on the screen
- public class MyActivity extends Activity {
- public void OnStart () {
- Requestscreenfeatures (Feature_badge_image);
- Super.onstart ();
- Setbadgeresource (Android. R.drawable.sym_def_app_icon);
- }
- }
- Application-system-defined standard "Green background" visual processing
- public class MyActivity extends Activity
- public void OnStart () {
- Super.onstart ();
- SetTheme (Android. R.style.theme_black);
- }
- }
4. Referencing resources within an XML file
1) Referencing a custom resourceandroid:text= "@string/hello" here uses the "@" prefix to introduce a reference to a resource-the text that follows in the @[package:]type/name form is the name of the resource. In this case, we do not need to specify the package name because we are referencing the resources in our own package. The type is the XML child node name, and name is the XML attribute name:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <resources>
- <string name= "Hello" >hello World, hellodemo!</string>
- </resources>
2) referencing system resourcesAndroid:textcolor= "@android: color/opaque_red" Specifies package:android
3) Referencing theme propertiesAnother resource value allows you to reference the value of a property in the current theme. This property value can only be used in style resources and XML attributes, and it allows you to change the appearance of UI elements by changing them to the standard changes provided by the current theme, rather than providing specific values. Android:textcolor= "? Android:textdisabledcolor" Note that this is very similar to resource references, except that we use a "?" Prefix instead of "@". When you use this tag, you provide the name of the property resource, which will be looked up in the subject-because the resource tool knows the required property resource, so you do not need to display the claim type (if the declaration, its form is? Android:attr/android: Textdisabledcolor). In addition to using the identifier of this resource to query the values in the topic instead of the original resource, its naming syntax is the same as "@":? [Namespace:]type/name, this type is optional.] 5. Replacing resources (for replaceable resources and configurations) the personal understanding of this replacement resource is mainly used to adapt to a variety of screen sizes, as well as internationalization. For this part of the content, please refer to http://androidappdocs.appspot.com/guide/topics/resources/resources-i18n.html, later study! 6. Color Value Syntax:
- <color name= "Color_name" > #color_value </color>
Can be saved in res/values/colors.xml (filename can be arbitrary). XML reference: android:textcolor= "@color/color_name" java reference: int color = Resources.getcolor (r.color.color_name) where #color_ Value has the following format (a for alpha channel): #RGB #argb#rrggbb#aarrggbb XML Example (declares two colors, first opaque, second transparent color):
- <?xml version= "1.0" encoding= "Utf-8"?>
- <resources>
- <color name= "opaque_red" > #f00 </color>
- <color name= "translucent_red" > #80ff0000 </color>
- </resources>
7.Color drawables Syntax:
- <drawable name= "Color_name" >color_value</drawable>
Can be saved in Res/values/colors.xml. XML reference: android:background= "@drawable/color_name" java reference: drawable reddrawable = resources.getdrawable ( R.drawable.color_name) Color_name the same as above. Personally, the color attribute is used in general, and the Drawable property is used when paintdrawable is needed. XML Example:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <resources>
- <drawable name= "opaque_red" > #f00 </drawable>
- <drawable name= "translucent_red" > #80ff0000 </drawable>
- </resources>
8. The picture is usually placed inside the res/drawable/. Official hints PNG (preferred), JPG (acceptable), GIF (discouraged), it seems that the general use of PNG format is better! The XML Reference @[package:]drawable/some_filejava reference R.drawable.some_file reference is without the extension 9. Dimension syntax:
- <dimen name= "Dimen_name" >dimen_value units </dimen>
Generally saved as res/values/dimen.xml. Unit of measure:
px(pixels): The actual pixels of the screen, often said resolution 1024*768pixels, is the transverse 1024px, portrait 768px, different devices display the same effect.
inch(inch): The physical size of the screen, equal to 2.54 centimeters per inch.
mm(mm): The physical size of the screen.
PT(point): The physical size of the screen. 1/72 inches.
Dp/dip: pixels independent of density, a unit of abstraction based on screen density. 1DP = 1px on a monitor 160 dots per inch. However, the ratio of DP and PX varies with the screen density, and different devices have different display effects.
SP: pixels independent of scale, primarily used for font display of best for TEXTSIZE, as a unit of text-related size. Xml:android:textsize= "@dimen/some_name"
Java:float dimen = resources.getdimen (r.dimen.some_name) XML Example:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <resources>
- <dimen name= "One_pixel" >1px</dimen>
- <dimen name= "Double_density" >2dp</dimen>
- <dimen name= "SIXTEEN_SP" >16sp</dimen>
- </resources>
String below is an official example of the right/wrong:
- You need to enclose the entire string in double quotation marks without using escape characters
- <string name= "Good_example" > "This's ll work" </string>
- Using escape characters
- <string name= "good_example_2" >this\ ' ll also work</string>
- Error
- <string name= "Bad_example" >this won ' t work!</string>
- Error cannot use HTML escape character
- <string name= "bad_example_2" >xml encodings won't work either!</string>
You can use HTML tags for formatted strings, such as setting colors in some text in a string. For this type of string, some processing is required and the XML cannot be referenced by other resources. The official gave an example to compare the use of ordinary string and formatted string:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <resources>
- <string name= "Simple_welcome_message" >Welcome!</string>
- <string name= "Styled_welcome_message" >we is <b><i>so</i></b> glad to see you.</ String>
- </resources>
XML code
- <textview
- Android:layout_width= "Fill_parent"
- android:layout_height= "Wrap_content"
- android:textalign= "Center"
- android:text= "@string/simple_welcome_message"/>
Java code
- Assign a styled string resource to a TextView.
- Charsequence str = getString (r.string.styled_welcome_message);
- TextView TV = (TextView) Findviewbyid (R.id.text);
- Tv.settext (str);
In addition to the style/format of the string processing, it is a little trouble. The official gave an example:
- <?xml version= "1.0" encoding= "Utf-8"?>
- <resources>
- <string name= "Search_results_resultstextformat" >%1$d results for <b>&quot;%2$s& Quot;</b></string>
- </resources>
The%1$d here is a decimal number, and%2$s is a string. When we assign a string to%2$s, we need to use the
HtmlEncode (String)The function handles that string:
- Title is the string we want to assign to%2$s.
- String escapedtitle = Textutil.htmlencode (title);
Then use String.Format () to implement the assignment, and then use Fromhtml (string) to get the formatted string:
- String Resultstextformat = GetContext (). Getresources (). getString (R.string.search_results_resultstextformat);
- String Resultstext = String.Format (Resultstextformat, Count, Escapedtitle);
- Charsequence styledresults = html.fromhtml (Resultstext);
Assets folder resource Access Assets folder files are kept in the original file format, you need to use Assetmanager as a byte stream to read the file. 1. First Call in activity
getassets ()To get the Assetmanager reference. 2. Re-use the Assetmanager
Open (String fileName, int accessmode)The InputStream method specifies the read file and the access mode to get the input stream. 3. Then you read the file with the InputStream of the open file, and remember to InputStream when the read is complete.
Close ()。 4. Call Assetmanager.
Close ()Close the Assetmanager. It is important to note that files from resources and assets can only be read and cannot be written.
The following is read from the raw file:
Code
Public String Getfromraw () {
try {
InputStreamReader Inputreader = new InputStreamReader (Getresources (). Openrawresource (R.raw.test1));
BufferedReader bufreader = new BufferedReader (Inputreader);
String line= "";
String result= "";
while (line = Bufreader.readline ()) = null)
Result + = line;
return Result;
} catch (Exception e) {
E.printstacktrace ();
}
}
The following is read directly from assets
Code
public string Getfromassets (string fileName) {
try {
InputStreamReader Inputreader = new InputStreamReader (Getresources (). Getassets (). Open (FileName));
BufferedReader bufreader = new BufferedReader (Inputreader);
String line= "";
String result= "";
while (line = Bufreader.readline ()) = null)
Result + = line;
return Result;
} catch (Exception e) {
E.printstacktrace ();
}
}
Of course, if you want to get the memory stream, you can return to the memory stream directly! This article from http://blog.sina.com.cn/s/blog_7161d18a0100o7go.html
Android reads files from assets and res (GO)