在代码中使用资源在代码中使用资源需要知道完整的资源 ID 和您的资源对象类型。下面是资源引用语法: R.resource_type.resource_name 或者 android.R.resource_type.resource_name resource_type 是 R 类中保存制定类型资源的子类。 resource_name 是定义在 XML 文件中的资源名或者被其他文件类型所定义的资源文件名(无扩展名) 每一类型的资源都依据其类型,被添加入某一指定的R子类;学习R子类的类型关系请参考资源引用一节。您的应用程序引用已被编译的资源时可以不带包名(比如R.resource_type.resource_name)。Android包含一个标准资源的序号,比如屏幕的样式和按钮的背景。引用这些资源,您必须使用带android的语法,比如android.R.drawable.button_background。 下面是一些好的和不好的代码例子: // Load a background for the current screen 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.