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

时间:2021-05-15 13:28:34

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.

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

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

这里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>

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

但是,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>

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

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

Thanks much.

非常感谢。

1 个解决方案

#1


34  

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.

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

See "Referencing Style Attributes" here: accessing-resources

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

#1


34  

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.

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

See "Referencing Style Attributes" here: accessing-resources

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