我是靠谱客的博主 故意红酒,最近开发中收集的这篇文章主要介绍android 如何调用布局,Android布局中使用的@、@+、?和*的详细描述,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

1、@[:][/]和 @+[:][/], @android:id

简单的概括就是:

@[表示引用本应用中类型为reource_type的叫resource_name的资源。

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

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

@+[:][表示新增加到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:

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、?[:][/]

简单的概括就是:

:]

[表示使用当前应用使用的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:

[:][/]

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:

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 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、*[:][/]

简单的概括就是:

*[:]

[表示引用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引用该资源

最后

以上就是故意红酒为你收集整理的android 如何调用布局,Android布局中使用的@、@+、?和*的详细描述的全部内容,希望文章能够帮你解决android 如何调用布局,Android布局中使用的@、@+、?和*的详细描述所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部