What's the difference between @android: and ?a

2019-01-23 08:40发布

问题:

This question already has an answer here:

  • What's the difference between “?android:” and “@android:” in an android layout xml file? 1 answer

What's the difference between

android:color="@android:color/black"

and

style="?android:attr/borderlessButtonStyle" 

What's the difference between the @ and ? ?

This is one of those questions which is ungoogleable, or ogooglebar.

回答1:

(from this answer and its comments)

Prefixing the ID with a question mark indicates that you want to access a style attribute that's defined in a style theme, rather than hard-coding the attribute, as explained in Referencing Style Attributes.

More specifically, the ? implies an extra level of indirection. Think of it as dereferencing a theme attribute to fetch the resource it points to rather than referring to the attribute itself. You see this with ?android:attr/foo



回答2:

@android:color/black

means you are referring to an color defined in the android namespace. This namespace is the namespace of the framework.

search black in this file: black color in framework

style="?android:attr/borderlessButtonStyle"

"?android:attr/borderlessButtonStyle" simply means "use the value defined by the attribute called borderlessButtonStyle in the namespace android." This attribute and its value are part of the Android framework, the "android" namespace.

borderlessButtonStyle in framework


Edited: from this Referencing Style Attributes

this link tell us:

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


回答3:

here Google has a document on it http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes