styles.xml中使用的自定义attrs参数

Lam*_*gaR 7 android

我在attrs.xml中定义了一组自定义Android布局参数.现在我想在styles.xml文件中使用一些标签.

目前我收到此错误:

error: Error: No resource found that matches the given name: attr 'custom:tag'
Run Code Online (Sandbox Code Playgroud)

我尝试声明自定义XML命名空间如下:

<resources
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.my.project"
>
Run Code Online (Sandbox Code Playgroud)

希望,这里可以应用每个布局声明中使用的相同逻辑,但没有成功.

Mar*_*lts 15

XML命名空间机制用于命名标记和属性.当您定义这样的样式时:

<?xml version="1.0" encoding="utf-8"?>
<resources
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.my.project">

    <style name="my_style"> <item name="custom:tag">some_value</item> </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

您正在尝试将XML命名空间应用于属性,这将无效.在这种情况下,您应该直接指定包名称,如下所示:

    <style name="my_style"> <item name="com.my.project:tag">some_value</item> </style>
Run Code Online (Sandbox Code Playgroud)

现在Android将能够解析定义属性的位置.