【问题标题】:Android Styles: Difference between 'style="@android:style/XYZ"' and 'style="?android:attr/XYZ"'?Android 样式:'style="@android:style/XYZ"' 和 'style="?android:attr/XYZ"' 之间的区别?
【发布时间】:2012-06-22 14:35:52
【问题描述】:

我正在尝试设置按钮的样式,使其看起来像我在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 类别又是用来做什么的?

谢谢!

【问题讨论】:

    标签: android android-layout android-resources android-styles


    【解决方案1】:

    来自another answer 上的 SO(此处转述):

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

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

    Referencing Style Attributes

    【讨论】:

      【解决方案2】:

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

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

      这在内部取消引用在 appcompat/res/values/themes.xml 文件中定义的 @android:style/Widget.Button。

      (?android:) - 始终链接到应用的当前主题的属性值。 因此允许我们仅引用 android 定义的样式,而不是创建一个新样式并提供硬编码的值。

      谷歌文档说,"It uses the style that is defined by this attribute, in the current theme"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-26
        • 2015-02-06
        • 2011-09-02
        • 2015-10-28
        • 2013-09-18
        • 1970-01-01
        相关资源
        最近更新 更多