gpt4 book ai didi

Android 访问 XML 中的 SDK 属性

转载 作者:行者123 更新时间:2023-11-29 20:00:24 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com