【问题标题】:Android accessing SDK attributes in XMLAndroid 访问 XML 中的 SDK 属性
【发布时间】:2016-04-01 20:00:10
【问题描述】:

我正在尝试扩展Android的simple_list_item_2,所以我复制了它的XML,即:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/listPreferredItemHeight"
    android:mode="twoLine"
    android:paddingStart="?attr/listPreferredItemPaddingStart"
    android:paddingEnd="?attr/listPreferredItemPaddingEnd">

    <TextView android:id="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textAppearance="?attr/textAppearanceListItem" />

    <TextView android:id="@id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:layout_alignStart="@id/text1"
        android:textAppearance="?attr/textAppearanceListItemSecondary" />

</TwoLineListItem>

问题是,在我的实现中,这条线

    android:textAppearance="?attr/textAppearanceListItemSecondary"/>

给出错误:

Cannot resolve symbol ?attr/textAppearanceListItemSecondary

我试过这样的东西:

android:textAppearance="android:?attr/textAppearanceListItemSecondary"

android:textAppearance="@android?attr/textAppearanceListItemSecondary" 但它不起作用

所以我的问题是,如何访问 Android SDK 资源中的布局可以访问的相同符号/属性?我是否需要将符号复制到我的应用程序目录中,还是可以直接访问 simple_list_item_2 所做的那个?

另外,原始的simple_list_item_2 位于C:\Android\sdk\platforms\android-23\data\res\layout\,如果有帮助,我的实现位于\app\src\main\res\layout

【问题讨论】:

  • 呃,我刚刚用谷歌搜索了这个,来到这里,然后意识到这是我去年问的自己的问题

标签: android xml android-layout attributes


【解决方案1】:
android:textAppearance="?android:attr/textAppearanceListItemSecondary"

Documentation

你需要将?放在包名之前,而不是之后:?[&lt;package_name&gt;:][&lt;resource_type&gt;/]&lt;resource_name&gt;

引用样式属性
样式属性资源允许您引用当前应用主题中的属性值。引用样式属性允许您通过设置样式以匹配当前主题提供的标准变体来自定义 UI 元素的外观,而不是提供硬编码的值。引用样式属性本质上是说,“在当前主题中使用由该属性定义的样式。”

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

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

【讨论】:

  • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
  • 嗨Соколов,欢迎来到 Stack Overflow!关于你的回答;您的代码可能是正确的,但在某些上下文中它会做出更好的答案;例如,您可以解释这个提议的更改如何以及为什么会解决提问者的问题,可能包括指向相关文档的链接。这将使它对他们更有用,对正在寻找类似问题的解决方案的其他网站读者也更有用。
【解决方案2】:

您无法访问私有 SDK 属性,因此您必须复制它们。

在使用反射的代码中可能是可能的,但在 xml 中肯定不行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多