2011-01-23 106 views
38

在android layout xml文件中,“?android:”和“@android:”有什么区别?它们似乎是重用android SDK资源的可互换方式。在android layout xml文件中,“?android:”和“@android:”有什么区别?

我发现的唯一区别是由以下示例说明的。

这里的TextView的右边缘与呈现的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> 

这里左边缘对齐,但是,TextView的右边缘与RelativeLayout的右边缘对齐。 TextView与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> 

这两个布局的唯一区别是使用@android vs?android。两种编译都没有错误。

非常感谢。

+0

[(?)问号Android版XML属性(可能的重复http://stackoverflow.com/questions/2733907/question-mark-in- xml-attributes-for-android) – 2016-02-20 04:06:18

回答

33

用问号标记ID的前缀表示您想要访问在样式主题中定义的样式属性,而不是对属性进行硬编码。

请参阅 “引用样式属性” 在这里:accessing-resources

+4

更具体地说,意味着额外的间接水平。可以将它视为取消引用主题属性以获取其指向的资源,而不是引用该属性本身。你用'?android:attr/foo`来看这个。 – adamp 2011-01-23 03:22:17

相关问题