【问题标题】:Why does a sample use ? when referencing style listSeparatorTextViewStyle? [duplicate]为什么要使用样本?引用样式 listSeparatorTextViewStyle 时? [复制]
【发布时间】:2014-08-15 06:57:47
【问题描述】:

我想使用一种风格的 Android 库。我想我应该使用@(案例2)作为style属性,但我从网站上看到了一个示例,它使用?(案例1)。我不知道为什么。

Android 库是否包含名为 listSeparatorTextViewStyle 的资源和名为 listSeparatorTextViewStyle 属性的样式?是的,我确实在系统 android lib attrs.xml 中找到了名为 listSeparatorTextViewStyle 属性的样式,但是在哪里可以找到名为 listSeparatorTextViewStyle 的资源?

这里是代码和效果

案例 1

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    style="?android:attr/listSeparatorTextViewStyle"  
/>

案例 2

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    style="@android:attr/listSeparatorTextViewStyle"  
/>

【问题讨论】:

标签: android android-xml android-styles


【解决方案1】:

引用资源属性:

  • 可以使用 R.java 中的整数从代码中引用资源,例如 R.drawable.myimage
  • 可以使用特殊的 XML 语法从资源中引用资源,例如 @drawable/myimage

引用样式属性:

  • A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."

  • To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional.

  • ?[&lt;package_name&gt;:][&lt;resource_type&gt;/]&lt;resource_name&gt;

EX::

   <EditText id="text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="?android:textColorSecondary"
    android:text="@string/hello_world" />
  • Here, the android:textColor attribute specifies the name of a style attribute in the current theme. Android now uses the value applied to the android:textColorSecondary style attribute as the value for android:textColor in this widget. Because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.

参考:: Android Docs - 就在那里

【讨论】:

    【解决方案2】:

    第一个有趣的问题!

    我查看了R.stylable 的参考指南,它在文本的几个部分中提到了以下内容:

    必须是对另一个资源的引用,格式为 @[+][package:]type:name 或格式为 ?[package:][type:]name 的主题属性。

    找到this page后得到了完整的解释:

    以下是在 XML 资源中引用 资源的语法

    @[<package_name>:]<resource_type>/<resource_name>
    
    • &lt;package_name&gt; 是资源所在包的名称(引用同一个包的资源时不需要)
    • &lt;resource_type&gt; 是资源类型的 R 子类
    • &lt;resource_name&gt; 是不带扩展名的资源文件名或 XML 元素中的 android:name 属性值(对于简单值)。

    要引用 style 属性,名称语法几乎与普通资源格式相同,但不是使用 at 符号 (@),而是使用问号 (? ),并且资源类型部分是可选的。例如:

    ?[<package_name>:][<resource_type>/]<resource_name>
    

    结论:在引用XML 中的资源 时使用@,在引用样式属性 时使用?。如果您没有全部了解,我强烈建议您阅读Accessing Resources guide

    【讨论】:

    • 谢谢!您的意思是 android lib 包含名为 listSeparatorTextViewStyle 资源和名为 listSeparatorTextViewStyle 属性的样式吗?是的,我确实在系统 android lib attrs.xml 中找到了名为 listSeparatorTextViewStyle 属性的样式,但是在哪里可以找到名为 listSeparatorTextViewStyle 的资源?
    猜你喜欢
    • 2011-07-11
    • 2021-01-01
    • 2021-10-14
    • 1970-01-01
    • 2019-05-20
    • 2021-08-16
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多