如何在android代码中调用资源

1,688次阅读
没有评论

Using Resources in Code
Access resources in code using the static R class. R is a generated class based on your external
resources, and created when your project is compiled. The R class contains static subclasses for each
of the resource types for which you’ve defi  ned at least one resource. For example, the default new
project includes the R.string and R.drawable subclasses.
If you use the ADT plug-in in Eclipse, the R class will be created automatically
when you make any change to an external resource fi le or folder. If you are not
using the plug-in, use the AAPT tool to compile your project and generate the R
class. R is a compiler-generated class, so don’t make any manual modifi  cations
to it because they will be lost when the fi  le is regenerated.
Each of the subclasses within R exposes its associated resources as variables, with the variable names
matching the resource identifi  ers — for example, R.string.app_name or R.drawable.icon.
The value of these variables is an integer that represents each resource’s location in the resource
table, not an instance of the resource itself.
Where a constructor or method, such as setContentView, accepts a resource identifi  er, you can
pass in the resource variable, as shown in the following code snippet:

// Inflate a layout resource.
setContentView(R.layout.main);
// Display a transient dialog box that displays the
// error message string resource.
Toast.makeText(this, R.string.app_error, Toast.LENGTH_LONG).show();
When you need an instance of the resource itself, you need to use helper methods to extract them
from the resource table. The resource table is represented within your application as an instance of
the Resources class.
These methods perform lookups on the application’s current resource table, so these helper methods
can’t be static. Use the getResources method on your application context, as shown in the follow-
ing snippet, to access your application’s Resources instance:
Resources myResources = getResources();

The Resources class includes getters for each of the available resource types and generally works by
passing in the resource ID you’d like an instance of. The following code snippet shows an example
of using the helper methods to return a selection of resource values:
Resources myResources = getResources();
CharSequence styledText = myResources.getText(R.string.stop_message);
Drawable icon = myResources.getDrawable(R.drawable.app_icon);
int opaqueBlue = myResources.getColor(R.color.opaque_blue);
float borderWidth = myResources.getDimension(R.dimen.standard_border);
Animation tranOut;
tranOut = AnimationUtils.loadAnimation(this, R.anim.spin_shrink_fade);
ObjectAnimator animator =
(ObjectAnimator)AnimatorInflater.loadAnimator(this,
R.anim.my_animator);
String[] stringArray;
stringArray = myResources.getStringArray(R.array.string_array);
int[] intArray = myResources.getIntArray(R.array.integer_array);

Frame-by-frame animated resources are infl  ated into AnimationResources. You can return the
value using getDrawable and casting the return value, as shown here:
AnimationDrawable androidAnimation;
androidAnimation =
(AnimationDrawable)myResources.getDrawable(R.drawable.frame_by_frame);

 

正文完
 

公众号