开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1103|回复: 1
收起左侧

[开发工具] Android----xml文件中的控件的id设置

[复制链接]
发表于 2017-6-6 10:34:27 | 显示全部楼层 |阅读模式   北京市北京市
本帖最后由 yuyu5 于 2017-6-6 10:37 编辑

上课了讲师指出,Android开放中要想得到布局文件中控件的引用,该控件必须设置id属性,这两有两种方式设置id:(1)@+id/xxxx;(2)@id/xxxx;下面做个简单的介绍。
1、@+id/xxx:如果R文件中没有该id则创建;
注意:一个xml文件中不能出现两个以该形式设置同一id的两个控件(include标签例外);
示例1 正确的使用:
  1. <TextView
  2. android:id="@+id/mytv"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="hello world"/>
复制代码



示例2 错误(两个id相同):此时系统会提醒报错
  1. <TextView
  2. android:id="@+id/mytv"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="hello world"/>
  6. <TextView
  7. android:id="@+id/mytv"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="hello world"/>
复制代码



2.@id/xxxx:引用ids.xml中相应的id,与@+id/xxx不同,一旦向ids.xml文件中添加一个id在R.java文件中会生成一个相应的id,无论是否有控件使用该id。
使用示例:
(1)创建ids.xml
  1. <resources>
  2. <item name="hello" type="id" />
  3. <item name="hello2" type="id" />
  4. <item name="hello3" type="id" />
  5. <item name="hello4" type="id" />
  6. <item name="hello5" type="id" />
  7. <item name="hello6" type="id" />
  8. <item name="hello7" type="id" />
  9. <item name="hello8" type="id" />
  10. </resources>
复制代码



(2)使用id
  1. <TextView
  2. android:id="@id/hello"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="hello 1" />

  6. <TextView
  7. android:id="@id/hello"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="hello 2" />

  11. <TextView
  12. android:id="@id/hello"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="hello 3" />
复制代码

多个控件可以以同样的方式设置统一id,但是该id只属于最先使用该id的控件。
发表于 2017-6-9 22:55:37 | 显示全部楼层   山西省运城市
原来是这样,一直用的@+id
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表