欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android编程中@id和@+id的区别分析

时间:2021-04-01 08:18:05|栏目: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
**
**   http://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,则在layout中可如下定义@id/price_edit,否则@+id/price_edit

简单来讲:

@+id 新增一个资源id
@id和android:id,引用现有的资源id

希望本文所述对大家Android程序设计有所帮助。

上一篇:Android 事件分发详解及示例代码

栏    目:Android代码

下一篇:Android开发之超强图片工具类BitmapUtil完整实例

本文标题:Android编程中@id和@+id的区别分析

本文地址:http://www.codeinn.net/misctech/92270.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有