欢迎来到 Android人的问与答 ,在这里提问或帮助他人解答。 Question2Answer的1.6.x和1.7.x版本的中文翻译包我修改更新:

Question2answer

如何使用中文语言包

Q2A 1.6.x - Chinese Simplified (简体中文)

Q2A 1.7.x - Chinese Simplified (简体中文)


国内服务器推荐 阿里云服务器
国外服务器推荐 DigitalOcean
VPN推荐 正版Green VPN
阿里云推荐码:ldvq50

Android布局中使用的@、@+、?和*的详细描述

0 喜欢 0 不喜欢
237 浏览

之前在网上看到很多朋友对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.

 

 

最新提问 1月 18, 2015 分类:Android View | 用户: forlong401 (7,050 分)

1个回答

0 喜欢 0 不喜欢

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月 18, 2015 用户: forlong401 (7,050 分)
...