출처 : http://developer.android.com/intl/ko/guide/topics/resources/accessing-resources.html#ResourcesFromXml
Android source code에서 외부 package의 resource를 접근하고자 할 경우에는, Package Manager를 통해서 resource를 얻어와서 접근할 수 있다.
하지만 xml 등의 resource file에서 외부 package의 resource를 접근할 때는 아래와 같이, package 명을 추가해서 접근한다.
@[<package_name>:]<resource_type>/<resource_name>
몇 몇 가지 방법을 시도해 봤으나, 전혀 엉뚱한 package에 대해서는 될 턱이 없고 (resource를 알 수 없다는 에러 발생) , 하나의 프로그램 작성 중 여러개의 package를 사용하는 경우 등에 한해 사용이 가능하다.
Accessing Resources from XML
You can define values for some XML attributes and elements using a reference to an existing resource. You will often do this when creating layout files, to supply strings and images for your widgets.
For example, if you add a Button
to your layout, you should use a string resource for the button text:
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/submit" />
Syntax
Here is the syntax to reference a resource in an XML resource:
@[<package_name>:]<resource_type>/<resource_name>
<package_name>
is the name of the package in which the resource is located (not required when referencing resources from the same package)<resource_type>
is theR
subclass for the resource type<resource_name>
is either the resource filename without the extension or theandroid:name
attribute value in the XML element (for simple values).
See Resource Types for more information about each resource type and how to reference them.
Use cases
In some cases you must use a resource for a value in XML (for example, to apply a drawable image to a widget), but you can also use a resource in XML any place that accepts a simple value. For example, if you have the following resource file that includes a color resource and a string resource:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="opaque_red">#f00</color> <string name="hello">Hello!</string> </resources>
You can use these resources in the following layout file to set the text color and text string:
<?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/opaque_red" android:text="@string/hello" />
In this case you don't need to specify the package name in the resource reference because the resources are from your own package. To reference a system resource, you would need to include the package name. For example:
<?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@android:color/secondary_text_dark" android:text="@string/hello" />
Note: You should use string resources at all times, so that your application can be localized for other languages. For information about creating alternative resources (such as localized strings), see Providing Alternative Resources.
You can even use resources in XML to create aliases. For example, you can create a drawable resource that is an alias for another drawable resource:
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/other_drawable" />
This sounds redundant, but can be very useful when using alternative resource. Read more about Creating alias resources.