Introduction to the main resource files and folders in Android

  • 2020-05-17 06:20:42
  • OfStack

In the Android project folder, the main resource files are located in the res folder

1:assets folder is a native file that is not compiled, that is, the files in this folder will not be like xml, java files are precompiled, can store 1 pictures, html, js, css and other files.
2: the introduction of multiple folders in the res folder

res/anim/ XML files, which are compiled into frame-by-frame animation (frame by frame animation) or tweening animation (tweened animation) objects res/drawable/.png,.9.png,.jpg files, which are compiled into the following Drawable resource subtypes, one of which can be obtained by using Resource.getDrawable (id); To get 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, an PNG with true color but no need for 256 colors might be converted to an 8-bit PNG with a palette. This allows images of the same quality to take up fewer resources. So we have to be aware that these binary images that are in this directory may change as they are generated. If you want to read a bitstream and convert it to a bitmap (bitmap), put the image file in the res/raw/ directory to avoid being automatically optimized.

res/layout/ XML files that are compiled into the screen layout (or part 1 of the screen)

res/values/ XML files that can be compiled into many types of resources. Note: unlike other res/ folders, it can hold any number of files that hold a 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 named any way you want, here are some typical files (file naming convention is to include element types in the name) :

array.xml defines an array

colors.xml defines the string values of color drawable and colors (color string values)

These resources are obtained using Resource.getDrawable () and Resources.getColor (), respectively.

dimens.xml defines the size value (dimension value).

Get these resources using Resources.getDimension ().

strings.xml defines the string (string) value

Access these resources using Resources.getString () or Resources.getText ().

styles.xml defines style (style) objects

res/xml/ any XML file that can be read at run time by calling Resources.getXML ().

res/raw/ copy directly to any file on the device. They do not need to be compiled; they are added to the compressed files that your application compiles. To use these resources, call Resources.openRawResource (), which takes the ID of the resource, R.raw.somefilename.

The automatically generated R class has an in the gen folder of the project folder. The resources we normally refer to refer mainly to the variables of this class. Note: the R class is automatically generated and cannot be manually modified. When a resource changes, it is automatically modified.

R.resource_type.resource_name or android.resource_type.resource_name where resource_type is a subclass of R, saving a specific type of resource. resource_name is the name property of a resource defined in the XML file, or the file name of another file type defined for the resource (not including the extension, which refers to the icon.png similar file in the drawable folder, name=icon). Android contains many standard resources, such as screen styles and button backgrounds. To refer to these resources in your code, you must use android, limit, such as android. R. drawable. button_background.

Reference resources within the xml file

1) reference custom resources

android:text=" @string /hello"; the "@" prefix is used to introduce a reference to a resource -- the text following the @[package:]type/name form is the name of the resource. In this case, we don't need to specify the package name, because we are referring to resources in our own package. type is the child node name of xml, name is the property name of xml: Hello World, HelloDemo!

2) reference system resource android:textColor=" @android :color/opaque_red" specify package: android

3) reference topic property another resource value allows you to reference the value of a property in the current topic. This property value can only be used in the style resource and XML properties; Instead of providing specific values, it allows you to change the appearance of UI elements by changing them to the standard changes provided for the current topic. android: textColor = "? android:textDisabledColor" note that this is very similar to a resource reference, except that we use 1 "?" The prefix replaces the "at sign". When you use this tag, you provide the name of the property resource, which will be looked up in the topic -- because the resource tool knows the required property resource, you don't need to declare the type (if declared, in the form of? android: attr/android: textDisabledColor). In addition to using the identifier of this resource to query for the value in the subject instead of the original resource, the nominalization syntax and the "@" form 1 to:? [namespace:]type/name, type optional here.

Color Value syntax: # color_value can be stored in the res/values/colors xml (file name can be arbitrary).

xml quote: android:textColor=" @color /color_name"

Java reference: int color = Resources. getColor(R. color. color_name)

Among them, #color_value has the following format (A stands for Alpha channel) : #RGB #ARGB #AARRGGBB xml example (declare two colors, the first opaque, the second transparent) : #f00 #80ff0000

Color Drawables syntax: color_value can be stored in the res/values/colors xml.

xml quote: android:background=" @drawable /color_name"

java reference: Drawable redDrawable = Resources.getDrawable (R.drawable.color_name) color_name and 1 above.

Picture 1 in res/drawable/ png (preferred), jpg (acceptable), gif (discouraged), it seems to be better to use png (png).

xml reference @ drawable/some_file [package:]

The java reference R.drawable.some_file reference is with no extension

dimension syntax: dimen_value units 1, save for res/values/dimen xml.

Unit of measurement:

px(pixel): the actual pixel of the screen. The resolution is usually 1024*768pixels, which means 1024px horizontally and 768px vertically. The display effect of different devices is the same. in(inch): the physical size of the screen, equal to 2.54 cm per inch. mm(mm): physical size of the screen. pt(dot) : physical size of the screen. 1/72 of an inch. dp/dip: density-independent pixel, an abstract unit based on screen density. On a 160-point per inch monitor, 1dp = 1px. However, the ratio of dp to px varies with the screen density, and different devices have different display effects. sp: pixel independent of scale, mainly used for font display best for textsize, as a unit of size relative to text.

XML: android:textSize="@dimen/some_name"

Java: float dimen = Resources.getDimen(R.dimen.some_name)

Access to assets folder resources files in the assets folder are kept in the original file format, requiring AssetManager to read the files in a byte stream.
1. First call getAssets() in Activity to get the AssetManager reference.
2. open(String fileName, int accessMode) method of AssetManager specifies the file to be read and the access mode to get the input stream InputStream.
3. Then read the file with inputStream, which has open file, and remember inputStream.close ().
4. Close AssetManager by calling AssetManager.close ().

Related articles: