編輯:關於 Android 程式設計
本文分析了 Android 程式設計中 @id 與 @+id 的區別,供大家參考。
Android 中的組件需要用一個 int 型別的值來表示,這個值就是組件標籤中 id 屬性的值。
id 屬性只能接受資源型別的值,也就是必須以 @ 開頭,例如 @id/abc、@+id/xyz 等。
如果在 @ 後面使用「+」,表示當修改並儲存某個佈局檔後,系統會自動在 R.java 檔中產生對應的 int 型別變數,變數名就是「/」後面的值。例如 @+id/xyz 會在 R.java 中產生 int xyz = value,其中 value 是一個十六進位數。若 xyz 在 R.java 中已存在同名變數,就不再產生新變數,而該組件會使用這個已存在變數的值。
既然組件的 id 屬性只要是資源 id 就行,那麼自然可以設定任何已存在的資源 id,例如 @drawable/icon、@string/ok、@+string/you 等;也可以設定 Android 系統中已存在的資源 id,例如 @id/android:list。那麼這個 android 是什麼意思呢?其實這個 android 就是系統 R 類(在 R.java 中)所在的 package。
我們可以在 Java 程式碼編輯區輸入 android.R.id.,就會列出相應的資源 id,例如也可以把 id 屬性值設為 @id/android:message。
另外還有一種查看系統中已定義 id 的方法:進入 sdk/platforms/android-16/data/res/values 目錄下的 ids.xml 檔:
<?xml version="1.0" encoding="utf-8"?> <!-- ** ** Copyright 2007, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** ** you may not use this file except in compliance with the License. ** ** You may obtain a copy of the License at ** ** https://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ --> <resources> <item type="id" name="background" /> <item type="id" name="checkbox" /> <item type="id" name="content" /> <item type="id" name="empty" /> <item type="id" name="hint" /> <item type="id" name="icon" /> <item type="id" name="icon1" /> <item type="id" name="icon2" /> <item type="id" name="input" /> <item type="id" name="left_icon" /> <item type="id" name="list" /> <item type="id" name="menu" /> <item type="id" name="message" /> <item type="id" name="primary" /> <item type="id" name="progress" /> <item type="id" name="right_icon" /> <item type="id" name="summary" /> <item type="id" name="selectedIcon" /> <item type="id" name="tabcontent" /> <item type="id" name="tabhost" /> <item type="id" name="tabs" /> <item type="id" name="text1" /> <item type="id" name="text2" /> <item type="id" name="title" /> <item type="id" name="title_container" /> <item type="id" name="toggle" /> <item type="id" name="secondaryProgress" /> <item type="id" name="lock_screen" /> <item type="id" name="edit" /> <item type="id" name="widget_frame" /> <item type="id" name="button1" /> <item type="id" name="button2" /> <item type="id" name="button3" /> <item type="id" name="extractArea" /> <item type="id" name="candidatesArea" /> <item type="id" name="inputArea" /> <item type="id" name="inputExtractEditText" /> <item type="id" name="selectAll" /> <item type="id" name="cut" /> <item type="id" name="copy" /> <item type="id" name="paste" /> <item type="id" name="copyUrl" /> <item type="id" name="selectTextMode" /> <item type="id" name="switchInputMethod" /> <item type="id" name="keyboardView" /> <item type="id" name="closeButton" /> <item type="id" name="startSelectingText" /> <item type="id" name="stopSelectingText" /> <item type="id" name="addToDictionary" /> <item type="id" name="accountPreferences" /> <item type="id" name="smallIcon" /> <item type="id" name="custom" /> <item type="id" name="home" /> <item type="id" name="fillInIntent" /> <item type="id" name="rowTypeId" /> <item type="id" name="up" /> <item type="id" name="action_menu_divider" /> <item type="id" name="icon_menu_presenter" /> <item type="id" name="list_menu_presenter" /> <item type="id" name="action_menu_presenter" /> <item type="id" name="overflow_menu_presenter" /> <item type="id" name="popup_submenu_presenter" /> </resources>
若在 ids.xml 中已定義了某個 ID,則在佈局中可直接用 @id/price_edit 引用,否則要用 @+id/price_edit 來新增。
簡單來說:
@+id 表示新增一個資源 id;
@id 和 android:id 則用來引用現有的資源 id。
希望本文所述能對大家的 Android 程式設計有所幫助。
Android TextView 文字描邊(字幕)效果的實作範例
想讓 TextView 的文字帶上描邊、呈現類似字幕的效果嗎?透過自訂一個繼承自 TextView 的元件,並在 onDraw 中搭配陰影與裁剪,就能輕鬆做到。本文分享一
Android 聯絡人按字母索引快速查找實作
效果目標聯絡人清單若有大量資料,通常需要字母索引來加速查找。這個範例會在清單右側顯示 A 到 Z 的索引,當使用者滑動或點擊索引時,ListView 會快速跳到對應位置,
CollapsingToolbarLayout 完全解析:打造折疊式 Toolbar
簡介 在各種應用中,你大概見過這樣的效果:Toolbar 一開始是透明的,襯著一張背景大圖與大標題;隨著頁面向下滑動,標題逐漸縮小並收攏到 Toolbar 上,而背景圖片
自定義 View 實戰:仿去哪兒網按壓顯示指紋並縮放的效果
仔細看去哪兒網 App 的首頁,那些 Win8 風格的方塊在按下時有兩個細節很打眼:圖片會微微縮放,同時浮現一層指紋圖案,真的像手指按在玻璃上一樣,質感立刻拉滿。今天我們