Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "?android:" and "@android:" in an android layout xml file?

What's the difference between "?android:" and "@android:" in an android layout xml file? They seem to be interchangeable ways of reusing android SDK resources.

The only difference I discovered is illustrated by the following example.

Here the TextView's right edge is rendered aligned with the left edge of the ImageButton

 <RelativeLayout     android:id="@id/header"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="#888888">     <TextView         android:id="@android:id/text1"         android:layout_alignParentLeft="true"         android:text="blah blah"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_toLeftOf="@android:id/button1" />     <ImageButton         android:id="@android:id/button1"         android:layout_alignParentRight="true"         style="@style/PlusButton" /> </RelativeLayout> 

Here, however, the right edge of the TextView is aligned with the right edge of the RelativeLayout. The TextView overlaps the ImageButton.

<RelativeLayout     android:id="@id/header"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="#888888">     <TextView         android:id="@android:id/text1"         android:layout_alignParentLeft="true"         android:text="blah blah"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_toLeftOf="?android:id/button1" />     <ImageButton         android:id="?android:id/button1"         android:layout_alignParentRight="true"         style="@style/PlusButton" /> </RelativeLayout> 

The only difference between the two layouts is the use of @android vs ?android. Both compile with no errors.

Thanks much.

like image 704
Julian A. Avatar asked Jan 23 '11 01:01

Julian A.


People also ask

What is the difference between APP and android in XML?

android is usually used for attribute coming from Android SDK itself. app is often used if you are using the support library. You may also see other namespaces if you are using custom views (of your own or form a library). Thanks.

What is android XML in android?

XML tags define the data and used to store and organize data. It's easily scalable and simple to develop. In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy. XML only contains tags, while implementing they need to be just invoked.

Why layouts are created using XML file in android?

Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML — with a series of nested elements. Each layout file must contain exactly one root element, which must be a View or ViewGroup object.


1 Answers

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.

See "Referencing Style Attributes" here: accessing-resources

like image 185
Jason LeBrun Avatar answered Oct 02 '22 13:10

Jason LeBrun