Android 访问 XML 中的 SDK 属性

标签 android xml android-layout attributes

我正在尝试扩展 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"/>

给出错误:

无法解析符号?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:textAppearance="?android:attr/textAppearanceListItemSecondary"

Documentation

您需要放置 ?在包名称之前,而不是之后: ?[<package_name>:][<resource_type>/]<resource_name>

Referencing style attributes
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. For instance:

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

关于Android 访问 XML 中的 SDK 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36364873/

相关文章:

Android 将一个 View 置于另一个之上

android - 如何允许 View 超出设备尺寸 - android

java - 错误 : Class not found when unmarshalling: com. facebook.login.Login 客户端请求

xml - XSL-FO:显示阿拉伯字符

c# - 检查 xml 中的文本值

xml - 使用完整声明将 xml 插入数据库

java - 将字符串转换为颜色 int

java - Android工作室:Configure Project in android studio

android - 如何将按钮添加到android软键盘?

java - 如何在 gridLayout 中以编程方式设置列号和行号?