您的位置:首页 > 移动开发 > Android开发

Android xml资源文件中@、@android:type、@*、?、@+含义和区别

2013-10-24 21:19 459 查看
之前在网上看到很多朋友对android布局文件中的@、@+、?和*不能清晰的理解和使用。在网上搜了一下,也没有看到很好的文章对这个问题进行解决。所以,我研究整理了他们的关系和使用方法,同时附上原始的出处。以便想更深入研究的朋友考究。英文大家找个翻译软件比对着看吧。概括的如有不当还请包涵指正。先谢过啦。

1、@[<package_name>:][<resource_type>/]<resource_name>和 @+[<package_name>:][<resource_type>/]<resource_name>, @android:id

简单的概括就是:

@[<resource_type/],resource_name>表示引用本应用中类型为reource_type的叫resource_name的资源。

@[<package_name>:][<resource_type/],resource_name>表示引用packgage_name这个包中类型为reource_type的叫resource_name的资源。这里的包一般就是framework中的android。

@+[<resource_type/],resource_name>表示新增加到本应用中类型为reource_type的叫resource_name的资源,通过aapt会自动生成一个整型的ID和它对应。

@+[<package_name>:][<resource_type/],resource_name>表示新增加到package_name的包中类型为reource_type的叫resource_name的资源,通过aapt会自动生成一个整型的ID和它对应。

要了解更详细的信息可参看SDK文档/docs/guide/topics/ui/declaring-layout.html

ID

Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute.
This is an XML attribute common to all View objects (defined by the View class) and you will use it very often. The syntax for an ID, inside an XML tag is:

android:id="@+id/my_button"

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. Theplus-symbol
(+)
means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID,
you do not need the plus-symbol, but must add the android package namespace, like so:

android:id="@android:id/empty"

With the android package namespace in place, we're now referencing an ID from the android.R resources class, rather than the local resources class.

In order to create views and reference them from the application, a common pattern is to:

1. Define a view/widget in the layout file and assign it a unique ID:

<Button android:id="@+id/my_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/my_button_text"/>

2. Then create an instance of the view object and capture it from the layout (typically in the onCreate() method):

Button myButton = (Button) findViewById(R.id.my_button);

Defining IDs for view objects is important when creating a RelativeLayout. In a relative layout, sibling views can define their layout relative to another sibling view, which is referenced by the unique ID.

An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).

要了解更详细的信息可参看SDK文档/docs/reference/android/view/View.html

View IDs need not be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching.

Tags

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them
in a separate structure.

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

简单的概括就是:

?<package_name>:] [<resource_type/],resource_name>表示使用当前应用使用的theme中的packgage_name这个包中类型为 reource_type的叫resource_name的资源。

要了解更详细的信息可参看SDK文档/docs/guide/topics/resources/accessing-resources.html

A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme,
instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?) , and the resource type portion
is optional. For instance:

?[<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.
http://www.linuxtopia.org/online_books/android/devguide/guide/topics/ui/themes.html
Notice the use of the at-symbol (@) and the question-mark (?) to reference resources. The at-symbol indicates that we're referencing a resource previously defined elsewhere (which may be from this project or from the Android framework). The question-mark indicates
that we're referencing a resource value in the currently loaded theme. This is done by referring to a specific <item> by its name value. (E.g., panelTextColor uses the same color assigned to panelForegroundColor, defined beforehand.) This technique can be
used only in XML resources.

3、*[<package_name>:][<resource_type>/]<resource_name>

简单的概括就是:

*[<package_name>:] [<resource_type/],resource_name>表示引用packgage_name这个包中类型为 reource_type的叫resource_name的资源。这里引用的一般是framework未公开的资源,即:android框架的内部资源。代码中对应的引用是com.android.internal.R.xx.xxx 。因为不是公开的接口或者资源,所以建议不使用这样的资源

要了解更详细的信息可参看:
http://stackoverflow.com/questions/3886873/androiddrawable-ic-vs-androiddrawable-ic
The @*android is used to access private resources. These resources can change or be removed between two versions of Android so you should NEVER use them. This is for framework use only. android内部资源, 通过com.android.internal.R.id.XXX引用该资源

一.@代表引用资源

1.引用自定义资源。格式:@[package:]type/name

android:text="@string/hello"

2.引用系统资源。格式:@android:type/name

android:textColor="@android:color/opaque_red"

注意:其实@android:type/name是@[package:]type/name 的一个子类

二.@*代表引用系统的非public资源。格式:@*android:type/name

系统资源定义分public和非public。public的声明在:

<sdk_path>\platforms\android-8\data\res\values\public.xml

@*android:type/name:可以调用系统定义的所有资源

@android:type/name:只能够调用publi属性的资源。

注意:没在public.xml中声明的资源是google不推荐使用的。

三.?代表引用主题属性

另外一种资源值允许你引用当前主题中的属性的值。这个属性值只能在style资源和XML属性中使用;它允许你通过将它们改变为当前主题提供的标准变化来改变UI元素的外观,而不是提供具体的值。例如:

android:textColor="?android:textDisabledColor"

注意,这和资源引用非常类似,除了我们使用一个"?"前缀代替了"@"。当你使用这个标记时,你就提供了属性资源的名称,它将会在主题中被查找,所以你不需要显示声明这个类型(如果声明,其形式就是?android:attr/android:textDisabledColor)。除了使用这个资源的标识符来查询主题中的值代替原始的资源,其命名语法和"@"形式一致:?[namespace:]type/name,这里类型可选。

四.@+代表在创建或引用资源 。格式:@+type/name

含义:”+”表示在R.java中名为type的内部类中添加一条记录。如"@+id/button"的含义是在R.java 文件中的id 这个静态内部类添加一条常量名为button。该常量就是该资源的标识符。如果标示符(包括系统资源)已经存在则表示引用该标示符。最常用的就是在定义资源ID中,例如:

@+id/资源ID名 新建一个资源ID

@id/资源ID名 应用现有已定义的资源ID,包括系统ID

@android:id/资源ID名 引用系统ID,其等效于@id/资源ID名

android:id="@+id/selectdlg"

android:id="@android:id/text1"

android:id="@id/button3"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐