本文来自网易云社区

作者:孙有军

在开发Andorid应用程序中,经常会自定义View来实现各种各样炫酷的效果,在实现这吊炸天效果的同时,我们往往会定义很多attr属性,这样就可以在XML中配置我们想要的属性值,以下就是定义属性值可能遇到的各种坑。

大家都知道怎么定义attr属性,一般如下:

<declare-styleable name="Sample">
   <attr name="custom" format="string|reference" /></declare-styleable>

先声明一个styleable名称,name名称最好见名知义,一个styleable里面可以有多个attr属性,每一个attr都含有一个name,同时需要指明所能赋值的类型,这是是依靠format来定义的。定义好之后就可以在自定义View中使用,来实现各种吊炸天的效果,使用如下:
xml中使用:

<com.sample.ui.widget.Custom   android:id="@+id/custom_view"   android:layout_width="130dp"   android:layout_height="130dp"   android:layout_gravity="center_horizontal"   android:layout_marginTop="90dp"   app:text="@string/custom_desc"
   />

记得声明 xmlns:app="http://schemas.android.com/apk/res-auto", app 可以随便取名
代码中获取值:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Sample);
String value = a.getString(R.styleable.Sample.custom);
a.recycle();

根据format不同,还有getDimension,getColor等方式获取值。

上面只是描述了一般定义的方式,但他不是今天的主题,今天的主题是可能遇到的各种坑:

1:项目中只包含一个attr.xml,出现 Attribute "custom" has already been defined,参考链接

<declare-styleable name="Sample">
        <attr name="custom" format="string|reference" /></declare-styleable><declare-styleable name="Sample1">
        <attr name="custom" format="string|reference" /></declare-styleable>

如上声明了两个styleable,同时包含了相同的属性custom,这时在编译时会提示Attribute "xxx" has already been defined,表示相同属性重复定义,相同styleable name不能再同一个attr.xml中重复定义,styleable name不一致attir也不能重复定义,attr format属性不影响重复定义结果。因此可以采用如下方法解决该问题:

a:重命名相同属性名,将其中一个改为不同的名字
b:提取重复定义attr,作为公共属性,方式如下:

<attr name="custom" format="string|reference" /><declare-styleable name="Sample">
    <attr name="custom" /></declare-styleable><declare-styleable name="Sample1">
    <attr name="custom" /></declare-styleable>

2: 项目中引用了多个外部项目,出现 Attribute "custom" has already been defined
不同的导入项目中,可能包含多个attr.xml,这样在定义时极有可能重复定义,他又分为如下两种情况:

a: 主项目,引用库包含同名styleable name,如:
主项目:

<declare-styleable name="Sample">
    <attr name="custom" /></declare-styleable>

引用库:

<declare-styleable name="Sample">
    <attr name="custom" /></declare-styleable>

这种情况下,编译是不会出现错误的,可以正常编译。

b: 主项目,引用库包含不同名styleable,但是有同名attr,如;
主项目:

<declare-styleable name="Sample">
    <attr name="custom" /></declare-styleable>

引用库:

<declare-styleable name="Sample1">
    <attr name="custom" /></declare-styleable>

编译时会出现 Attribute "custom" has already been defined。由此可以得出,在项目中引用各种库,模块时,各个不同的模块定义attr,要遵循以下规则,
1:全部不能重复定义,全部不能重复很难实现,不同的团队,不同的产品是极有可能重复定义,因此该方式很难实现。
2:各个不同模块,定义时加上模块前缀,这种方式重复几率就小很多,编译时再将重复的重命名就ok了。

网易云免费体验馆,0成本体验20+款云产品!

更多网易研发、产品、运营经验分享请访问网易云社区

相关文章:
【推荐】 GDB抓虫之旅(上篇)
【推荐】 客户端SDK测试思路

Andorid自定义attr的各种坑的更多相关文章

  1. 手把手教你自定义attr

    最近在学习的过程中遇到了自定义的attr和自定义的style.因此各种百度,各种博客的学习,算是有了一个系统的了解.在这里记录下自己的收获. 一.为什么要使用自定义attr以及本文定位 在androi ...

  2. Android 自定义 attr

    好纠结,弄了一个下午老是报错如是总结一下安卓自定视图和自定义属性. (一)自定义属性 在Values文件下建立一个attrs.xml文件,attr的format可以参考:http://www.cnbl ...

  3. 深入理解Android 自定义attr Style styleable以及其应用

    相信每一位从事Android开发的猿都遇到过需要自己去自定义View的需求,如果想通过xml指定一些我们自己需要的参数,就需要自己声明一个styleable,并在里面自己定义一些attr属性,这个过程 ...

  4. andorid 自定义view属性declare-styleable

    1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...

  5. C++ sort使用自定义函数的一些坑

    先看代码: 解释:使用自定义比较函数时,如果用了返回值恒为$true$或者恒为$false$的比较函数,就会这样子. 原因: https://stackoverflow.com/questions/4 ...

  6. django 自定义过滤器中的坑.

    今天在创建自定义过滤器的时候,设置已正常.但是在运行后报: 'filter' is not a valid tag library: Template library filter not found ...

  7. andorid 自定义seekbar

    效果如图: <?xml version="1.0" encoding="utf-8"?> <resources> <style n ...

  8. andorid——自定义seekbar(转)

    xml 新建=>xml=>values <?xml version="1.0" encoding="utf-8"?> <resou ...

  9. 自定义topo遇到的坑

    错误:TypeError: __init__() got an unexpected keyword argument 'delay' 解决办法:在创建topo的地方加一个link=TCLink即可, ...

随机推荐

  1. 牛客网多校训练第一场 A - Monotonic Matrix(Lindström–Gessel–Viennot lemma)

    链接: https://www.nowcoder.com/acm/contest/139/A 题意: 求满足以下条件的n*m矩阵A的数量模(1e9+7):A(i,j) ∈ {0,1,2}, 1≤i≤n ...

  2. 图片验证码——base64编码的使用

    一.介绍: 1.base64编码简介: Base64就是一种编码格式.Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成 ...

  3. [转]CUDA在Windows下的软件开发环境搭建

    引自:http://www.makaidong.com/yaoyuanzhi/archive/2010/11/13/1876215.html 本文我们以visual studio 2005 为例演示c ...

  4. supervisord常见问题

    第一种: 你在没有修改配置文件之前就已经启动:~supervisord -c supervisord.conf 然后又去修改配置文件,想重新读取一下但是却出现了以下报错: Another progra ...

  5. 在Mac终端中使用vim编辑文件步骤

    VIM (Unix及类Unix系统文本编辑器) Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.VIM是纯粹的自由软件. http://jingya ...

  6. New Language Features in C# 6

    Source:https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6 This document describes ...

  7. HTML5——前端预处理技术(Less、Sass、CoffeeScript)

    一.Less 1.1.概要 Less是一种动态样式语言,Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. Less 将 C ...

  8. jquery实现页面图片轮播

    1.创建一个html页面 <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...

  9. servelet跳转页面的路径中一直包含sevelet的解决办法

    解决办法1: 在web.xml配置文件中修改: <servlet-mapping> <servlet-name>LoginServelet</servlet-name&g ...

  10. Spring 整合Mybatis dao原始方法

    先看一下项目图,基本就理解了整合的内容 这次主角不再是Mybats的配置文件SqlMapConfig.xml了,而是Spring的applicationContext.xml applicationC ...