閑談Android中的@和?符號(hào)的引用區(qū)別
在 Android 項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常會(huì)用 “@” 或者 “?” 符號(hào)去引用系統(tǒng)或者我們應(yīng)用內(nèi)添加的資源,這兩種符號(hào)的引用有什么區(qū)別呢,“?attr/” 與 “?android:attr/” 之間又有怎樣的不同呢?本文我們不妨閑聊一下。
“@” 與 “?” 符號(hào)的引用在使用時(shí)都有一個(gè)規(guī)范的格式:"@[+][package:]type:name","?[package:][type:]name"??梢钥吹?,二者均包含引用符號(hào)、資源所屬的包、資源類(lèi)型和資源名稱(chēng)。
@ 資源引用
“@” 符號(hào)用于引用系統(tǒng)和我們?cè)陧?xiàng)目中添加的一些固有資源(drawable,string 等),或者定義的 style 樣式。比如:
- android:text="@string/app_name"
這里的 app_name 就是我們自己定義在項(xiàng)目文件 values/strings.xml 中的字符串資源。
- android:text="@android:string/cancel"
而這里的 cancel 屬于 Android SDK 中的系統(tǒng)字符串資源,所以需要添加@android: 來(lái)指明引用來(lái)源。android: 是 package: 的一個(gè)具體實(shí)例。
? 屬性引用
“?” 符號(hào)用于引用當(dāng)前主題中定義的一些屬性值。注意,“?” 符號(hào)通過(guò)屬性名字間接引用當(dāng)前主題中的對(duì)應(yīng)屬性值,而不是屬性本身。舉個(gè)例子:
- android:divider="?android:listDivider"
這里的 “?” 符號(hào)通過(guò)屬性名 android:listDivider 間接獲取當(dāng)前主題賦予該屬性的值。如同 @android: 一般,?android: 表示該值源自 Android SDK 系統(tǒng)屬性。由于在當(dāng)前主題中尋找對(duì)應(yīng)屬性名的值,所以沒(méi)有指定屬性類(lèi)型,其實(shí)等同于:?android:attr/listDivider。
那如何引用項(xiàng)目中自定義的屬性呢?我們?cè)?attrs.xml 中定義一個(gè)屬性,如:
- <declare-styleable name="CustomTextView">
- <attr name="colorTextCustom" format="reference|color"/>
- </declare-styleable>
顯然,此時(shí)我們定義的 colorTextCustom 屬性是沒(méi)有值的,直接引用沒(méi)有任何作用。需要在主題 style 中賦值:
- <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
- <item name="colorTextCustom">#FF0000</item>
- </style>
- <style name="AppTheme" parent="BaseTheme">
- <item name="android:textColor">?colorTextCustom</item>
- </style>
可以看到,這里在 BaseTheme 中對(duì) colorTextCustom 屬性賦值,并在 AppTheme 中通過(guò) “?colorTextCustom” 引用該屬性值。由于是本地項(xiàng)目中定義的屬性,所以沒(méi)有添加 android: 命名空間。其實(shí),這種做法的好處是,AppTheme 所覆蓋的 View 均可通過(guò)構(gòu)造函數(shù)獲取當(dāng)前主題中的 colorTextCustom 屬性值。
R.attr & R.style
Android SDK 中定義有很多屬性和主題可供使用,詳見(jiàn)官方文檔:R.attr &R.style。使用系統(tǒng)資源的好處就是,滿(mǎn)足不同系統(tǒng)的適配需求,較為靈活。
這里舉幾個(gè)常用的:
style="?android:attr/borderlessButtonStyle"
Android 5.0 默認(rèn) Button 的樣式自帶邊框陰影,可以使用這個(gè)系統(tǒng)樣式去除該樣式。當(dāng)然,這是單獨(dú)設(shè)置時(shí)的操作,為了方便全局控制,可以在 styles.xml 中自定義一個(gè)樣式,繼承一個(gè)無(wú)邊框樣式作為 parent:
- <style name="CustomBorderlessButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless">
- <item name="android:textColor">@android:color/white</item>
- ...
- </style>
android:background="?android:attr/selectableItemBackground"
可用于設(shè)置一些 List Item、Button之類(lèi)帶點(diǎn)擊效果的背景。該樣式自帶觸摸點(diǎn)擊效果,在 5.0 和更高版本上,更是附有 Ripple 漣漪效果,省去我們自己實(shí)現(xiàn) selector 選擇器的過(guò)程。當(dāng)然我們也可以自己使用 <ripple> 標(biāo)簽定義一個(gè) drawable 文件實(shí)現(xiàn)漣漪效果,只是需要注意版本限制。
android:background="?android:attr/dividerVertical"
實(shí)現(xiàn)分割線(xiàn)背景。
還有一些其他有用的系統(tǒng)資源,這里就不一一列舉了...