之前在网上看到很多朋友对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. The plus-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. Android中利用“反射”动态加载R文件中的资源
  2. [置顶] android中图片的三级cache策略(内存、文件、网络)之二:内存
  3. AndroidStudio mipmap图片大小
  4. Android资源管理框架(Asset Manager)简要介绍和学习计划
  5. android 获取网络图片缓存(内存—>文件—>网络)
  6. Android防止内存泄露
  7. Android用户界面设计学习之旅-第二站
  8. Android(安卓)加载不同 DPI 资源与内存消耗间的关系
  9. Android(安卓)Glide数据更新及内存缓存、硬盘缓存清理

随机推荐

  1. Android参数设置父布局集体宽高
  2. Android(安卓)N来电拦截
  3. Android(安卓)Afinal框架学习(一) FinalD
  4. android ApplicationInfo类
  5. 【Android】 Android中适配器简介
  6. Android:Groovy基础语法
  7. android中一个activity实现多个xml页面互
  8. android studio debug签名设置
  9. android日期控件显示
  10. android打开联系人的代码