What's the difference between “?android:” and

2019-01-13 09:45发布

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.

1条回答
Rolldiameter
2楼-- · 2019-01-13 10:39

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

查看更多
登录 后发表回答