Andorid自定义attr的各种坑
本文来自网易云社区
作者:孙有军
在开发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的各种坑的更多相关文章
- 手把手教你自定义attr
最近在学习的过程中遇到了自定义的attr和自定义的style.因此各种百度,各种博客的学习,算是有了一个系统的了解.在这里记录下自己的收获. 一.为什么要使用自定义attr以及本文定位 在androi ...
- Android 自定义 attr
好纠结,弄了一个下午老是报错如是总结一下安卓自定视图和自定义属性. (一)自定义属性 在Values文件下建立一个attrs.xml文件,attr的format可以参考:http://www.cnbl ...
- 深入理解Android 自定义attr Style styleable以及其应用
相信每一位从事Android开发的猿都遇到过需要自己去自定义View的需求,如果想通过xml指定一些我们自己需要的参数,就需要自己声明一个styleable,并在里面自己定义一些attr属性,这个过程 ...
- andorid 自定义view属性declare-styleable
1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...
- C++ sort使用自定义函数的一些坑
先看代码: 解释:使用自定义比较函数时,如果用了返回值恒为$true$或者恒为$false$的比较函数,就会这样子. 原因: https://stackoverflow.com/questions/4 ...
- django 自定义过滤器中的坑.
今天在创建自定义过滤器的时候,设置已正常.但是在运行后报: 'filter' is not a valid tag library: Template library filter not found ...
- andorid 自定义seekbar
效果如图: <?xml version="1.0" encoding="utf-8"?> <resources> <style n ...
- andorid——自定义seekbar(转)
xml 新建=>xml=>values <?xml version="1.0" encoding="utf-8"?> <resou ...
- 自定义topo遇到的坑
错误:TypeError: __init__() got an unexpected keyword argument 'delay' 解决办法:在创建topo的地方加一个link=TCLink即可, ...
随机推荐
- [Raspberry]001Ubuntu下查看linux版本,内核版本,系统位数,gcc版本
1. 查看linux版本 sunny@ubuntu:~$cat /etc/issueUbuntu 11.04 \n \l 2. 查看内核版本1) sunny@ubuntu:~$ cat /proc/ ...
- 以EF形式操作mysql数据库
1.引入Nuget包: 2.书写EF操作上下文 public class MySqlContext:DbContext { protected override void OnConfiguring( ...
- SecureCRT连接主机时(串口/网络),无法从键盘输入
Session Option-Connection-Serial-Flow Control,里面的选项全部取消掉,再重启CRT就ok了...
- asp.net mvc5 step by step(三)—— Entity Framework Database First
一.先建数据库. 打开,SQL Server Management 新建数据库Employee 并新建表 /****** Object: Table [dbo].[t_Employee] Script ...
- 初学JavaScript从入门到放弃(一)JavaScript介绍、变量、数据类型
一.JavaScript介绍 1.JavaScript:轻量级的客户端脚本语音 2.目前js已经不仅仅是客户语音,基于NODE可以做服务器端程序,所以Javascript是全栈编程语音 3.js及部分 ...
- Const 关键字详解
const 标识符 在c++中作为常量修饰符 用来修饰 函数 变量 指针 const 修饰的变量不可以改变值 const 在修饰指针的时候 const 标识符出现在*的左边表示 指向的变量为常量不能 ...
- 集群、RAC和MAA
集群:是一种由两台或多台节点机构成的松散耦合的计算节点集合,这个集合在整个网络中表现为单一的系统,并通过单一接口进行使用和管理.给用户提供网络服务或应用程序的单一视图.大多数模式下,集群中所有计算机都 ...
- TeamViewer13个人版使用中提示为商用版导致无法使用
前言:由于使用teamviewer个人免费版较频繁,被软件识别到不能再继续免费使用,无奈没有多余的资金进行购买正版软件, 通过鼓捣得到如下继续免费使用方案,整理下来以备不时之需,也可以被有同此困惑的朋 ...
- 多用户OFDM系统资源分配研究
首先,OFDMA 是什么? OFDM 技术的基本原理是将无线信道划分为若干互相正交的子信道,把高速串行数据流转化为低速并行子数据流,低速并行子数据流在子信道上独立传输. OFDMA 是LTE的下行多址 ...
- Java : Netty 入门案例
接收端代码: public class IOServer { public static void main(String[] args) throws IOException, Interrupte ...