Android查缺补漏(View篇)--布局文件中的“@+id”和“@id”有什么区别?
Android布局文件中的“@+id”和“@id”有什么区别?
- +id表示为控件指定一个id(新增一个id),如:
<cn.codingblock.view.customer_view.MyView
android:id="@+id/myview"
...
/>
- id表示引用一个现有的id,如:
<cn.codingblock.view.customer_view.MyView
android:id="@+id/myview"
android:layout_below="@id/btn_handle_myview"
.../>
但需要注意的是在布局文件中,被引用的id要在引用位置的上面,否则会编译出错,如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.codingblock.view.activity.MyViewActivity">
<cn.codingblock.view.customer_view.MyView
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btn_handle_myview"
android:layout_margin="10dp"
android:paddingLeft="15dp"
android:paddingRight="30dp"
android:background="#000"/>
<Button
android:id="@+id/btn_handle_myview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="移除、显示MyView" />
</RelativeLayout>
编译错误信息:
Error:(14, 31) No resource found that matches the given name (at 'layout_below' with value '@id/btn_handle_myview').
解决方法:
- 方法一:将引用id的位置改成+id,意思也就是说先将此id新增到工程的R文件中,如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.codingblock.view.activity.MyViewActivity">
<cn.codingblock.view.customer_view.MyView
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btn_handle_myview"
android:layout_margin="10dp"
android:paddingLeft="15dp"
android:paddingRight="30dp"
android:background="#000"/>
<Button
android:id="@+id/btn_handle_myview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="移除、显示MyView" />
</RelativeLayout>
在MyView的android:layout_below="@+id/btn_handle_myview"这行代码已经使用+id新增了btn_handle_myview这个id,下面再为Button指定id时用+id或者id都可以,因为此时R文件中已经有btn_handle_myview这个id,所以在为Button指定id时直接用"@id/btn_handle_myview"即使不带“+”号也不会报错。然而就算是带了“+”也不报错,而且也不会重复添加。
- 方法二:将引用id的代码放在+id的下面位置,如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.codingblock.view.activity.MyViewActivity">
<Button
android:id="@+id/btn_handle_myview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="移除、显示MyView" />
<cn.codingblock.view.customer_view.MyView
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btn_handle_myview"
android:layout_margin="10dp"
android:paddingLeft="15dp"
android:paddingRight="30dp"
android:background="#000"/>
</RelativeLayout>
这是一个小知识点,非常简单,但确是我们很容易忽略的一个地方,所以今天记录一下。
Android查缺补漏(View篇)--布局文件中的“@+id”和“@id”有什么区别?的更多相关文章
- Android查缺补漏(View篇)--自定义 View 的基本流程
View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...
- Android查缺补漏(View篇)--事件分发机制源码分析
在上一篇博文中分析了事件分发的流程及规则,本篇会从源码的角度更进一步理解事件分发机制的原理,如果对事件分发规则还不太清楚的童鞋,建议先看一下上一篇博文 <Android查缺补漏(View篇)-- ...
- Android查缺补漏(IPC篇)-- 款进程通讯之AIDL详解
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...
- Android查缺补漏(IPC篇)-- 进程间通讯之AIDL详解
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...
- Android查缺补漏(IPC篇)-- Bundle、文件共享、ContentProvider、Messenger四种进程间通讯介绍
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8387752.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...
- Android查缺补漏(IPC篇)-- 进程间通讯基础知识热身
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8479282.html 在Android中进程间通信是比较难的一部分,同时又非常 ...
- Android查缺补漏(IPC篇)-- 进程间通讯之Socket简介及示例
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8425736.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...
- Android查缺补漏(线程篇)-- IntentService的源码浅析
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8975114.html 在Android中有两个比较容易弄混的概念,Servic ...
- Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解
上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas和Paint,从细节上更进一步的讲解自定 ...
随机推荐
- Windows运行python脚本文件
开始学习python就是听说这个语言写脚本文件特别方便,简单使用.学了一段时间,但是直到现在我才直到直到怎么在Windows的cmd上运行脚本文件. 之前一直都是在pycharm上运行,并不实用. 百 ...
- react实现全选、取消全选和个别选择
react里面实现全选和取消全选,个别选择等操作,效果如下 代码: import React, {Component} from 'react' export default class Demo e ...
- scala - fold,aggregate,iterator
import org.json4s._ import org.json4s.jackson._ import org.json4s.jackson.JsonMethods._ import org.j ...
- 在Ubuntu中添加和删除PPA的软件源
PPA,英文全称为 Personal Package Archives,即个人软件包档案.是 Ubuntu Launchpad 网站提供的一项源服务,允许个人用户上传软件源代码,通过 Launchpa ...
- jquery表格展示
用bootstrap设计一个弹框,然后在弹框里面生成表格 <html> <head> <link rel="stylesheet" href=&quo ...
- windows底下怎么让cmder通过输入subl去打开sublime text
在window自身的cmd或者你安装的cmder中输入 doskey subl="D:\Program Files\sublime3\Sublime Text 3\sublime_text. ...
- /etc/redhat-release 查看centos 版本
查看centos 版本 [root@localhost ~]# cat /etc/redhat-release CentOS release 6.4 (Final)
- 划分LUN
划分LUN http://mp.weixin.qq.com/s?__biz=MzAwNzU3NzQ0MA==&mid=209842199&idx=1&sn=7d77fdf7a8 ...
- 原生的强大DOM选择器querySelector - querySelector和querySelectorAll
在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...
- 使用客户端等远程连接mysql数据库
1: 远程数据库(D1)数据: 数据库用户:root,数据库密码:root,数据库ip 内网地址 192.168.100.91,数据库端口 3306 本地主机:ip 192.168.127.1 ...