android布局xml文件中"?android:"和"@android:"之间有什么区别?

Jul*_* A. 42 layout android reference

android布局xml文件中"?android:"和"@android:"之间有什么区别?它们似乎是重用android SDK资源的可互换方式.

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

这里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>
Run Code Online (Sandbox Code Playgroud)

但是,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>
Run Code Online (Sandbox Code Playgroud)

两种布局之间的唯一区别是使用@android vs?android.两者都编译没有错误.

非常感谢.

Jas*_*run 36

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

请参阅此处的"引用样式属性":access-resources

  • 我希望在这个答案中有更多的解释 (8认同)
  • 更具体地说,?意味着额外的间接水平.可以将其视为取消引用主题属性以获取它指向的资源,而不是引用属性本身.你用`?android:attr/foo`看到这个. (4认同)