Overview:
An android application will contain many resource files, such as pictures, text, styles, layouts, etc. This blog mainly records access to resource files in the res directory in android.
The res directory usually has anim, animation, drawable, mipmap, layout, menu, raw, values and other subdirectories. I believe these file directories are familiar to android developers. Let’s summarize below,
The specific access methods of these files and the points that should be paid attention to, let us know more clearly why they should be used in this way.
In android development, these resource files are often used, and the most should be through R.<resourceType>.<filename> . This R is automatically generated by aapt when the application is compiled. The location of this R file is in app/build/intermediates/classes/<packagename>/R.class 。
You can see that the resources under res are all in R.class, which exist as static classes and static constants. When we use these resources, it is through this R class application.
There are two ways to access resources:
1. Use in Java code:R.drawable.abc_ab_share_pack_mtrl_alpha This can be applied to this abc_ab_share_pack_mtrl_alpha resource file.
2. Application in XML file: @drawable/abc_ab_share_pack_mtrl_alpha
Use case: set a picture for the ImageView
ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setImageResource(R.drawable.app_logo);
grammar:
[<package_name>.]R.<resource_type>.<resource_name>
<package_name>: Indicates the package name, if the application resource comes from its own resource package, it is not required.
<resource_type>: Indicates the R subtype.
<resource_name>: Resource file name.
There are many methods that accept resource ID parameters, and you can use Resources
the methods in to retrieve resources. You can Context.getResources()
get through Resources
the instance.
Examples of accessing resources in code:
// Load a background for the current screen from a drawable resourcegetWindow()
.setBackgroundDrawableResource
(R.drawable.my_background_image) ; // Set the Activity title by getting a string from the Resources object, because // this method requires a CharSequence rather than a resource IDgetWindow()
.setTitle
(getResources().getText
(R.string.main_title)); // Load a custom layout for the current screensetContentView
(R.layout.main_screen); // Set a slide in animation by getting an Animation from the Resources object mFlipper.setInAnimation
(AnimationUtils.loadAnimation(this, R.anim.hyperspace_in)); // Set the text on a TextView object using a resource ID TextView msgTextView = (TextView) findViewById(R.id.msg); msgTextView.setText
(R.string.hello_message);
Access resources in XML:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello"/>
Syntax :
@[<package_name>:]<resource_type>/<resource_name>
<package_name>: package name, resources in the same package are not required.
<resource_type>: Subtype in R.
<resource_name>: File name.
Reference system resources:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/secondary_text_dark_nodisable"/>
Reference style:
?[<package_name>:][<resource_type>/]<resource_name>
These parameters are the same as above, except that @ is replaced by?
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="?android:textColorSecondary"/>
Access platform resources:
Android includes many standard resources, such as styles, style themes, and layouts. To access these resources, qualify your resource references with android
package names. For example, you can add Android
The layout resources provided are used ListAdapter
for the list items in:
setListAdapter
(newArrayAdapter
<String>(this, android.R.layout.simple_list_item_1, myarray));
android . R . layout . simple_list_item_1 This is the layout resource defined ListView android platform.
The resource access in the res directory is about these things. Next write the type of resource