Android样式:'style="@android:style/XYZ"'和'style="?android:attr/XYZ"'之间的区别是什么?

13

我正在尝试样式化按钮,使其看起来像我在Android Full Width ICS style Minimalist Bottom ButtonsViews中所询问的那些按钮。

对于任何感兴趣的人,我已经成功地使用以下xml实现了这个效果:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:divider="@android:drawable/divider_horizontal_dark"
        android:gravity="bottom"
        android:orientation="vertical"
        android:paddingTop="16dip"
        android:showDividers="beginning|end" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:measureWithLargestChild="true"
            android:orientation="horizontal"
            android:divider="@android:drawable/divider_horizontal_dark"
            android:showDividers="middle" >                             


            <Button
                android:id="@+id/cancel_button"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:maxLines="2"
                android:text="@string/cancel_button" />

            <Button
                android:id="@+id/login_button"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:filterTouchesWhenObscured="true"
                android:maxLines="2"
                android:text="@string/login_button" />

        </LinearLayout>
    </LinearLayout>

不过我有一个问题。Eclipse的内容辅助工具似乎不知道以下资源解析的情况:

style="?android:attr/buttonBarButtonStyle"

我熟悉典型的解析方式(Eclipse的内容辅助工具也清楚),如下所示:

style=@android/style/...

...但我不明白两者之间的区别。似乎某些样式属性只出现在其中一个中。例如,以下内容无法解析为任何内容:

style=@android:attr/buttonBarStyle

这个也是一样:

style="@android:style/buttonBarStyle

所以这里有两个问题:

  1. 为什么资源引用语法不同?
  2. 为什么将样式混淆分类到“attr”类别下?
  3. “attr”类别到底有什么用途?

谢谢!

2个回答

13

以下是从SO(这里是简化版)上的another answer获得的信息:

在ID前使用问号意味着您想要访问在样式主题中定义的样式属性,而不是硬编码该属性。

可以将其视为取消引用主题属性以获取其指向的资源,而不是引用属性本身。

引用样式属性


1

?android:attr/buttonBarButtonStyle或者只有?android:buttonBarButtonStyle - (attr是可选的)

<item name="buttonBarButtonStyle">@android:style/Widget.Button</item>

这个内部解引用指向@android:style/Widget.Button,它在appcompat/res/values/themes.xml文件中定义。

(?android:) - 总是链接到当前应用的主题中属性的值。因此,我们可以仅引用Android定义的样式而不是创建新的并提供硬编码值。

Google文档说:"它使用由此属性在当前主题中定义的样式"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接