大家好,又見面了,我是你們的朋友全棧君。
我想問自定義View的時候,以下這段代碼,為何要寫兩次一樣的名稱呢?
我看了一些資料,說寫在declare-styleable系統會自動生成數組…..
我不太明白這實際應用是什麼?
如果說自動幫你生成了數組,友善使用,那寫在外面的三個又有什麼作用?
能從實際應用中講一講嗎?
<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<attr name=”titleText” format=”string” />
<attr name=”titleTextColor” format=”color” />
<attr name=”titleTextSize” format=”dimension” />
<declare-styleable name=”CustomTitleView”>
<attr name=”titleText” />
<attr name=”titleTextColor” />
<attr name=”titleTextSize” />
</declare-styleable>
</resources>
=========================================
那可以簡寫把?
<declare-styleable name=”CustomTitleView”>
<attr name=”titleText” format=”string” />
<attr name=”titleTextColor” format=”color” />
<attr name=”titleTextSize” format=”dimension” />
</declare-styleable>
自定義style
聲明和引用的關系,外面的三個是聲明了三個屬性的名稱及可指派類型,CustomTitleView則是引用了三個外面聲明的屬性,表明了CustomTitleView下有三個屬性。
應用時,比如在style.xml下引用到了CustomTitleView這個style,就可以對三個屬性指派,生成特定的style
釋出者:全棧程式員棧長,轉載請注明出處:https://javaforall.cn/158841.html原文連結:https://javaforall.cn