Android自定義attr的各種坑
在開發(fā)Android應(yīng)用程序中,經(jīng)常會(huì)自定義View來實(shí)現(xiàn)各種各樣炫酷的效果,在實(shí)現(xiàn)這吊炸天效果的同時(shí),我們往往會(huì)定義很多attr屬性,這樣就可以在XML中配置我們想要的屬性值,以下就是定義屬性值可能遇到的各種坑。
大家都知道怎么定義attr屬性,一般如下:
<declare-styleable name="Sample"> <attr name="custom" format="string|reference" /> </declare-styleable>
先聲明一個(gè)styleable名稱,name名稱最好見名知義,一個(gè)styleable里面可以有多個(gè)attr屬性,每一個(gè)attr都含有一個(gè)name,同時(shí)需要指明所能賦值的類型,這是是依靠format來定義的。定義好之后就可以在自定義View中使用,來實(shí)現(xiàn)各種吊炸天的效果,使用如下:
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();
根據(jù)format不同,還有g(shù)etDimension,getColor等方式獲取值。
上面只是描述了一般定義的方式,但他不是今天的主題,今天的主題是可能遇到的各種坑:
1:項(xiàng)目中只包含一個(gè)attr.xml,出現(xiàn) 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>
如上聲明了兩個(gè)styleable,同時(shí)包含了相同的屬性custom,這時(shí)在編譯時(shí)會(huì)提示Attribute “xxx” has already been defined,表示相同屬性重復(fù)定義,相同styleable name不能再同一個(gè)attr.xml中重復(fù)定義,styleable name不一致attir也不能重復(fù)定義,attr format屬性不影響重復(fù)定義結(jié)果。因此可以采用如下方法解決該問題:
a:重命名相同屬性名,將其中一個(gè)改為不同的名字
b:提取重復(fù)定義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: 項(xiàng)目中引用了多個(gè)外部項(xiàng)目,出現(xiàn) Attribute “custom” has already been defined
不同的導(dǎo)入項(xiàng)目中,可能包含多個(gè)attr.xml,這樣在定義時(shí)極有可能重復(fù)定義,他又分為如下兩種情況:
a: 主項(xiàng)目,引用庫包含同名styleable name,如:
主項(xiàng)目:
<declare-styleable name="Sample"> <attr name="custom" /> </declare-styleable>
引用庫:
<declare-styleable name="Sample"> <attr name="custom" /> </declare-styleable>
這種情況下,編譯是不會(huì)出現(xiàn)錯(cuò)誤的,可以正常編譯。
b: 主項(xiàng)目,引用庫包含不同名styleable,但是有同名attr,如;
主項(xiàng)目:
<declare-styleable name="Sample"> <attr name="custom" /> </declare-styleable>
引用庫:
<declare-styleable name="Sample1"> <attr name="custom" /> </declare-styleable>
編譯時(shí)會(huì)出現(xiàn) Attribute “custom” has already been defined。由此可以得出,在項(xiàng)目中引用各種庫,模塊時(shí),各個(gè)不同的模塊定義attr,要遵循以下規(guī)則,
1:全部不能重復(fù)定義,全部不能重復(fù)很難實(shí)現(xiàn),不同的團(tuán)隊(duì),不同的產(chǎn)品是極有可能重復(fù)定義,因此該方式很難實(shí)現(xiàn)。
2:各個(gè)不同模塊,定義時(shí)加上模塊前綴,這種方式重復(fù)幾率就小很多,編譯時(shí)再將重復(fù)的重命名就ok了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android TextView字幕效果實(shí)例
- Android 日歷控件庫,可左右滑動(dòng),顯示公歷,農(nóng)歷,節(jié)假日等功能
- Android開發(fā)之Picasso通過URL獲取用戶頭像的圓形顯示
- Android實(shí)現(xiàn)精確到天時(shí)分秒的搶購倒計(jì)時(shí)
- Android 系統(tǒng)實(shí)現(xiàn)多種開機(jī)動(dòng)畫和logo切換功能
- Android?jar庫源碼Bolts原理解析
- Android使用手勢(shì)監(jiān)聽器GestureDetector遇到的不響應(yīng)問題
- Android?狀態(tài)管理之Lifecycle淺析
相關(guān)文章
Android ListView 實(shí)例講解清晰易懂
這篇文章主要通過實(shí)例介紹了Android ListView,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Android常用的AlertDialog對(duì)話框及自定義對(duì)話框
本文主要介紹了android常用的AlertDialog對(duì)話框及自定義對(duì)話框的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04Android使用popupWindow仿微信彈出框使用方法
這篇文章主要為大家詳細(xì)介紹了Android使用popupWindow仿微信彈出框使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android登錄注冊(cè)功能 數(shù)據(jù)庫SQLite驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了Android登錄注冊(cè)功能,數(shù)據(jù)庫SQLite驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android應(yīng)用 坐標(biāo)系詳細(xì)介紹
這篇文章主要介紹了 Android 坐標(biāo)系的相關(guān)資料,本文對(duì)Android 坐標(biāo)系的知識(shí)做了詳細(xì)解讀,需要的朋友可以參考下2016-11-11Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)
這篇文章主要介紹了Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01從源代碼分析Android Universal ImageLoader的緩存處理機(jī)制
這篇文章主要介紹了從源代碼分析Android Universal ImageLoader的緩存處理機(jī)制 的相關(guān)資料,需要的朋友可以參考下2016-01-01Android自定義View實(shí)現(xiàn)彈性小球效果
前段時(shí)間看到一個(gè)功能,是一個(gè)小球沿著固定軌跡彈動(dòng)的效果,那么這篇文章小編給大家分享在Android中如何自定義View來實(shí)現(xiàn)彈性小球的效果,有需要的可以參考借鑒。2016-09-09