& ltitem & gtaaa & lt/item & gt;& ltit" />
1. string resource
& gt& gt 1. ordinary string
& gt& gt2. String array
Copy code
& lt resources & gt
& ltstring-array name = " planets _ array " >
& ltitem & gtaaa & lt/item & gt;
& ltitem & gtbbb & lt/item & gt;
& lt/string-array & gt;
& lt/resources & gt;
Copy code
Get method: getresources (). getstring array(r . array . planets _ array)。
& gt& gt3. Complex string resources
In some natural languages, different numbers have different uses, such as a book and two books. When the number is greater than 1, different nouns or other plural forms will be used;
Copy code
& lt resources & gt
& lt Complex name = "numberOfp">
& ltitem quantity="one "> one person & lt/item & gt;;
& ltitem quantity="other "> More people & lt/item & gt;;
& lt/ plural & gt
& lt/resources & gt;
Copy code
Besides one and others, the values of quantitative attributes can also be zero, two, fever and many;
Reference complex string:
//The reference number is 1.
getResources()。 getQuantityString(r . pluarlas . number ofp, 1);
//Complex strings with reference numbers of other values.
getResources()。 getQuantityString(r . pluarlas . number ofp, 10, 10);
& gt& gt4. Placeholder format string
There are three common ways to format strings:
& gt& gt 1. Use quotation marks in strings.
Although you can specify the values in the string at will, when you encounter special symbols (double quotation marks and single quotation marks), you need to take special measures to deal with these symbols.
If it is a single quotation mark ('), you can use an escape character (\) or double quotation marks (") to enclose the whole string. If it is a double quotation mark, you can use an escape character (\) before the double quotation mark.
& lt resources & gt
& ltstring name="str 1 ">" This will work "& lt/string & gt;; This will be useful.
& ltstring name="str2 "> This will work & lt/string & gt;; This will be useful.
& ltstring name="str3 ">\" Apple \ "& lt/string & gt;; "Apple"
& lt/resources & gt;
& gt& gt2. Format a string with a placeholder.
The String.format (string, object ...) method can format strings with placeholders, and string resources can format strings. The Format method needs% 1,%, ...,% nActually, the nth placeholder corresponds to n+ 1 parameter values of the format method;
& lt resources & gt
& lt! -$ s means that the placeholder is replaced by a string, and $d means that the placeholder is replaced by an integer->;
& ltstring name="str 1 "> Hello,% 1$s! You have %2$d new messages & lt/string & gt;;
& lt/resources & gt;
string str 1 = string . format(get resources()。 getString(R.string.str 1)," ly ", 17);
& gt& gt3. Format string resources with HTML tags.
The string resource supports some HTML tags, so you can use these HTML tags to format strings directly in the string resource.
String resources support the following HTML tags
& ltb> bold font
& lt I> Italic Settings
& ltu> underline words
Sometimes it is necessary to format strings with HTML tags and placeholders. If the string is formatted with the String.format method, all HTML tags in the string will be ignored. In order for the format method to format the band
HTML tags are really characters, and you need to use Html.formHTML method to process strings;
& lt resources & gt
& ltstring name = "hello _ world"> Welcome to<b & gtandroid & lt/b & gt;; & lt/string & gt;
& ltstring name="str2 "> Hello,% 1$s! You have & lt/b & gt;%2d new messages & lt/b > & lt/string & gt; & lt! -a string containing placeholders and html tags->;
& lt/resources & gt;
Because you need to use the Html.formHTML method to process strings,
Get string:
string text = string . format(get resources()。 getString(R.string.str2)," ly ", 10);
char sequence styledText = html . from html(text);
//If the parameters of format contain special characters of HTML, such as "
string escaped username = textutils . html encode(" ");
string text 1 = string . format(get resources()。 getString(R.string.str2)," ly ",20);
2. Layout resources
1, if the root node is a view, except
2. The root node is ViewGroup, and the commonly used layouts are subclasses of ViewGroup.
3. Reuse layout files
If you want to reuse layout files, you can use the.
& ltinclude layout = " @ layout/xx _ layout "/& gt;
If you want a layout file to be referenced by another layout file (use the
3. Image resources
Image files can be stored in image resources, and image resources in xml format can also be used to control the state and behavior of images.
& gt& gt 1. Common image resources
Drawable da = getResources()。 getDrawable(r . drawable . XXX);
& gt& gt2.xml image resources
The Xml image resource is actually an xml file specified in the drawable directory, which can additionally specify some properties of the image, such as the drawing and arrangement of the image.
& lt bitmap xmlns:android= ""
Android:src = " @ drawable/IC _ launcher "
Android:tile mode = " repeat " & gt;
& lt/bitmap & gt;
& gt& gt3. Nine patch image resources
Nine patch image resource files must take 9.png as the file extension, such as abc.9.png.
The main functions of this image resource are: to prevent a certain part of the image from being stretched; Determining the display position of the content in the control with the image as the background image;
The Android SDK itself provides a tool for Draw 9-patch. Start the \tools\draw9patch.bat command to start the tool.
Using this tool, a straight line with the thickness of 1 pixel can be drawn around the png map, and the straight lines on the upper edge and the left edge represent the potential range of the image in the horizontal and vertical directions respectively. If a certain area in the horizontal or vertical direction does not need to be stretched, the corresponding straight line may not be drawn; The straight lines on the right edge and the bottom edge represent the display range of the content in the control where the image is located, and the content is only displayed in the area where the straight lines are drawn on the right edge and the bottom edge. An important difference between the two straight lines representing the content display range and the stretching range is that the middle of the straight line representing the content display range cannot be broken, but the middle of the straight line representing the stretching range can be broken.
Nine patch picture resources are quoted in the same way as ordinary picture resources, and only the file name vitality.9.png is written when quoting;
& gt& gt4.XML nine patch image resources
Nine patch image resources also have corresponding xml image resources, so use
& gt& gt5. Layer resources
Layer resources are similar to
By default, the image will fill the range of the displayed image as much as possible, and the image may be stretched. To avoid stretching the image, you can use the
Copy code
& lt layer list xmlns:Android = "">;;
& lt project
Android:bottom=" 10dip "bottom pixel offset.
Android:left=" 10dip "Left offset pixel.
android:right=" 10dip " ...
Android:top = " 10 dip " & gt; ...
& lt bitmap
Android: Gravity = "Center"
Android:src = " @ drawable/hell "/& gt;
& lt/item & gt;
& lt/layer-list & gt;
Copy code
In some cases, you can use layers instead.
& gt& gt6. Image status resource, which deals with the display status of controls in different states.
Copy code
& lt selector xmlns:Android = "">;;
& ltitem Android:drawable = " @ drawable/BM " Android:state _ focused = " true " & gt; & lt/item & gt;
& ltitem Android:drawable = " @ drawable/BM " Android:state _ pressed = " true " & gt; & lt/item & gt;
& ltitem Android:drawable = " @ drawable/BM " & gt; & lt/item & gt;
& lt/selector & gt;
//Android: Set state _ focused/pressed to true, indicating that the drawable property of the current item is the drawable style when the focus is pressed.
Copy code
& gt& gt7. Image-level resources
The image resource state can only specify a limited number of states, and more states can be specified through the image level; The image level is an integer interval, and images in different states can be switched by ImageView.setImageLevel or Drawable.setLevel methods; Image-level resources are xml files, which must be used.
& lt level list xmlns:Android = "">;;
& ltitem Android:max level = " 2 " Android:min level = " 0 " Android:drawable = " @ drawable/hell "/& gt;
& ltitem Android:max level = " 4 " Android:min level = " 3 " Android:drawable = " @ drawable/hell "/& gt;
& lt/level-list & gt;
& gt& gt8. Cross-dilution of resources
Similarly, two images are switched (switching of more than two images is not supported), so that the switching of these two images has a fade-in and fade-out effect, for example, the light gradually becomes brighter or darker when switching;
& lt transition xmlns:Android = "">;;
& ltitem Android:drawable = " @ drawable/hell "/& gt;
& ltitem Android:drawable = " @ drawable/hell "/& gt;
& lt/transition & gt;
TransitionDrawable da =...;
//When switching from the first picture to the second picture, the time effect is 1 second.
da . start transition( 1000);
//When switching from the second picture to the first picture, the time effect is 1 second.
da . reverse transition( 1000);
& gt& gt9. Insert image resources.
Usage scenario: The image to be displayed is required to be smaller than the view where the image is loaded (the graphics are smaller than the view area), and it is also defined with xml resources, and only one node is inserted.
& ltinset xmlns:android= " "
Android:draw able = " @ draw able/hell "
Android:inset left = " 10 dip " & gt; & lt! -Distance extending to the left from the image->; Up/down/right distance->
& lt/inset & gt;
> >10. Clip image resources, which can only display part of the image, such as making a progress bar;
& ltclip xmlns:android= " "
Android: clipOrientation = "horizontal"//Specify the direction of the clip.
Android: drawable = "@ drawable/hell"//Specify the image to be edited.
Android:gravity = " left " & gt; //Specify the interception method, in this case, from the left.
& lt/clip & gt;
Editable cd =...;;
CD . set level( 1000);
Set level (level) above sets the width of the captured image. The default maximum value of ClipDrawable is 10000 (indicating no capture) and the minimum value is 0 (indicating no display).
& gt& gt 1 1. Zoom image resources.
& ltscale xmlns:android= " "
Android:draw able = " @ draw able/hell "
Android: scalegravity = "center"//Set the location where the picture is displayed.
Android:scaleHeight="70%" // Set the height of image display.
Android:scale width = " 80% " & gt; //Set the width of the image display.
& lt/scale & gt;
> >12. Shape resources
Copy code
& ltshape xmlns:android= " "
Android:shape = " rectangle " & gt; Shapes can be specified as rectangle, ellipse, line and ring.
& lt Corner & gt Define fillet.
& lt/corners & gt;
& lt gradient
android:angle="45 "
android:startColor="#000000 "
Android:end color = " # FFFFFF " & gt; Defines a color gradient from the lower left corner to the upper left corner.
& lt/gradient & gt;
& lt Fill & gt Defines the distance from the control content to the border.
& lt/padding & gt;
& lt strokes & gt define boundaries.
& lt/stroke & gt;
& lt solids & gt define filling.
& lt/solid & gt;
& ltsize & gt define the size.
& lt/size & gt;
& lt/shape & gt;
Copy code
13. Menu resources
Menus can be created not only by the code in the onCreateContextMenu or onCreateOptionsMenu method, but also by creating corresponding menu resource files in the res/menu directory, and menu resources can be loaded by the above two methods.
Menu resource file must start with
Copy code
@ Overlay
Public boolean once creation menu (
//Expand the menu; If the action bar exists, this will add items to the action bar.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main,menu);
Return true
}
Copy code
Check menuinflater.infect (int, menu).
Copy code
/**
* Expand the menu hierarchy from the specified XML resource.
*
* @param menuRes resource ID of the XML layout resource to be loaded (for example, < code & gtr.menu.main _ activity <; /code & gt; )
* @param menu The menu to expand to. Items and submenus will be added to the menu.
*/
public void inflate(int Menu,Menu menu) {
XmlResourceParser parser = null
Try {
parser = mContext.getResources()。 get layout(menuRes);
Attributes et attrs = xml.asattributeset (parser);
ParseMenu (parser, property, menu);
} catch ... finally {
If (parser! = null)parser . close();
}
}
Copy code
14. Style/theme
& gt& gt 1. style
Styles in android have the same function as styles in css. They are used to define the display style of interface elements, which are collections containing one or more control properties.
Defining styles needs to be defined in res/values/styles.xml. The following is the definition of styles:
& ltstyle name="textViewStyle " >
& ltitem name = " Android:text size " & gt; 22sp & lt/item & gt;
& ltitem name = " Android:text color " & gt; # FF0000 & lt/item & gt;
& lt/style & gt;
& ltstyle name = " textViewStyle 1 " parent = " textViewStyle " >& lt/style & gt; & lt! -this style inherits from textviewstyle->
& ltstyle name="textViewStyle。 Livingstone ">; & lt! -Another way to inherit the writing style, but not the definition style that comes with Android? -& gt;
& ltitem name = " Android:text color " & gt; # 00FF00 & lt/item & gt;
& lt/style & gt;
All defined styles will automatically generate a resource id in the R file. Adding a dot indicates that style inheritance will generate the resource ID shown in the above figure.
Reference to style:
& lt text view
style="@style/textViewStyle "
Android:layout _ width = " fill _ parent "
Android:layout _ height = " wrap _ content "
Android:text = " hello "/& gt;
& gt& gt2. Theme theme
Themes apply to the entire application or activity, and styles apply to specific controls. The application of theme is the same as style definition, except that theme can also set the display style of window; You need to refer to the reference of the topic in the manifest file. If it refers to the whole application, it needs to be configured in the application node; if it refers to a single activity, it only needs to be configured in this activity.
Copy code
& ltstyle name="Livingstonetheme " >& lt! -this definition is untitled topic->;
& ltitem name = " Android:window not title " & gt; true & lt/item & gt;
& ltitem name = " Android:window full screen " & gt; ? Android:window not title & lt; /item & gt;
& lt! -question mark refers to the value of the Android:window not title attribute in this topic->;
& ltitem name = " Android:text size " & gt; 18sp & lt; /item & gt;
& lt/style & gt;
Copy code
The android system defines some attributes, such as Android: theme = "@ Android: style/theme. Dialog box, which can make the activity look like a dialog box. More topics can be found in the document reference-> Android-& gt; View in R.style When the style attribute values in the theme conflict with those in the style, the values in the style will be displayed;
15. Other resources
Resource dimensions can also include dimensions, integers, Boolean values, integer arrays, resource arrays, and colors.
TypedArray ta = getResources()。 obtainTypedArray(int id); //Get array resources, including integer arrays and arrays.
Final summary:
Except the resource names in the res/values directory, resources in other directories will generate variables in the subclasses corresponding to the R class with file names; The resources in res/values will generate variables with the name attribute as the variable name in the subclasses corresponding to the R class;