android 调用资源语法

1.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 the R subclass for the resource type
  • <resource_name> is either the resource filename without the extension or the android:name attribute value in the XML element (for simple values).
<?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" />



<?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" />

2.Referencing style attributes

?[<package_name>:][<resource_type>/]<resource_name>

For example, here's how you can reference an attribute to set the text color to match the "primary" text color of the system theme:

<EditText id="text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="?android:textColorSecondary"
    android:text="@string/hello_world" />

Here, the android:textColor attribute specifies the name of a style attribute in the current theme.

Android now uses the value applied to the android:textColorSecondary style attribute as the value for android:textColor in this widget.

Because the system resource tool knows that an attribute resource is expected in this context,

you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.

 
 
posted @ 2017-11-14 14:33  Jokeyyu  阅读(218)  评论(0编辑  收藏  举报