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

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

目前我收到此错误

error: Error: No resource found that matches the given name: attr 'custom:tag'

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

<resources
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.my.project"
>

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

解决方法

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>

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

<style name="my_style"> <item name="com.my.project:tag">some_value</item> </style>

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...