Android自定义attr的各种坑

作者:xueshanhaizi 时间:2023-07-12 06:11:50 

在开发Android应用程序中,经常会自定义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=&rdquo;http://schemas.android.com/apk/res-auto&rdquo;, 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 &ldquo;custom&rdquo; 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 &ldquo;xxx&rdquo; 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 &ldquo;custom&rdquo; 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 &ldquo;custom&rdquo; has already been defined。由此可以得出,在项目中引用各种库,模块时,各个不同的模块定义attr,要遵循以下规则,
1:全部不能重复定义,全部不能重复很难实现,不同的团队,不同的产品是极有可能重复定义,因此该方式很难实现。
2:各个不同模块,定义时加上模块前缀,这种方式重复几率就小很多,编译时再将重复的重命名就ok了。

标签:Android,attr
0
投稿

猜你喜欢

  • 通过Html网页调用本地安卓(android)app程序代码

    2022-05-10 10:14:34
  • Android GPS详解及示例代码

    2021-11-12 08:27:28
  • 深入理解Java8新特性之接口中的默认方法和静态方法

    2023-11-24 01:44:25
  • java开发MVC三层架构上再加一层Manager层原理详解

    2023-06-14 06:10:51
  • springboot如何重定向外部网页

    2022-11-12 05:19:19
  • winfrom 在业务层实现事务控制的小例子

    2021-11-16 14:19:50
  • Java8简单了解Lambda表达式与函数式接口

    2022-11-07 00:22:31
  • JAVA如何定义构造函数过程解析

    2023-11-04 08:15:09
  • JAXB简介_动力节点Java学院整理

    2021-09-15 00:09:32
  • Spring深入刨析声明式事务

    2022-07-04 03:57:15
  • Java新特性之Nashorn_动力节点Java学院整理

    2022-07-31 17:18:13
  • Mybatis逆工程jar包的修改和打包

    2023-06-03 09:28:14
  • Apache SkyWalking 修复TTL timer 失效bug详解

    2023-08-18 08:09:53
  • 详解Java枚举为什么是单例模式的最佳选择

    2022-07-16 20:42:47
  • Kotlin Flow常用封装类StateFlow使用详解

    2022-07-16 03:46:28
  • C# LINQ to XML应用介绍

    2021-06-11 05:59:07
  • Java实现分页查询功能

    2023-03-03 14:30:19
  • SpringBoot中Dozer的使用小结

    2023-11-25 01:24:38
  • C# 拷贝数组的几种方法(总结)

    2023-09-14 06:50:17
  • Java编程Socket实现多个客户端连接同一个服务端代码

    2023-12-27 05:32:11
  • asp之家 软件编程 m.aspxhome.com