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”有什么区别?的更多相关文章

  1. Android查缺补漏(View篇)--自定义 View 的基本流程

    View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...

  2. Android查缺补漏(View篇)--事件分发机制源码分析

    在上一篇博文中分析了事件分发的流程及规则,本篇会从源码的角度更进一步理解事件分发机制的原理,如果对事件分发规则还不太清楚的童鞋,建议先看一下上一篇博文 <Android查缺补漏(View篇)-- ...

  3. Android查缺补漏(IPC篇)-- 款进程通讯之AIDL详解

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  4. Android查缺补漏(IPC篇)-- 进程间通讯之AIDL详解

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  5. Android查缺补漏(IPC篇)-- Bundle、文件共享、ContentProvider、Messenger四种进程间通讯介绍

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8387752.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  6. Android查缺补漏(IPC篇)-- 进程间通讯基础知识热身

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8479282.html 在Android中进程间通信是比较难的一部分,同时又非常 ...

  7. Android查缺补漏(IPC篇)-- 进程间通讯之Socket简介及示例

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8425736.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  8. Android查缺补漏(线程篇)-- IntentService的源码浅析

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8975114.html 在Android中有两个比较容易弄混的概念,Servic ...

  9. Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解

    上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas和Paint,从细节上更进一步的讲解自定 ...

随机推荐

  1. linux 系统信息查看

    查看系统版本:lsb_release -a 查看内核版本:uname -a 查看cpu型号:cat /proc/cpuinfo 查看硬盘空间情况df -lm 查看内存:free -m  VGA显卡:l ...

  2. java8新特性之Optional类

    NullPointException可以说是所有java程序员都遇到过的一个异常,虽然java从设计之初就力图让程序员脱离指针的苦海,但是指针确实是实际存在的,而java设计者也只能是让指针在java ...

  3. jquery 1.9 1.8 判断 浏览器(IE11,IE8,IE7,IE6)版本

    1.9以后很我方法删除了,所有和之前版本判断浏览器版本有所差记录一下 1.9 ------------------------------------------------------------- ...

  4. .NET程序运行过程

    ASP.NET http请求 - IIS - isapi.dll - CLR(com服务通信初始化CLR - AppDomain - 加载程序集 - JIT译 - 本地机器码 - 内存 - CPU 执 ...

  5. 44(function pointer 2)

    #include<iostream> using namespace std; class A { public: int x; int sayhello() { cout<< ...

  6. HTML标签_head标签

    HTML标签分为两种,自闭合标签和主动闭合标签:没有另一半 自动闭合标签,有另一半叫主动闭合标签. 自动闭合标签,只有开头没有结尾,即没有另一半:如<meta charset="UTF ...

  7. J - Fire!---UVA 11624

    题目链接 题意:J代表Joe的位置,F代表火的起点,下一刻火将会向四周扩散,求Joe逃离的最短时间,如果不能逃离输出IMPOSSIBLE; 注意火的起点可能不止一处 可以用两次bfs分别求出人到达某个 ...

  8. PULL解析学习

    学习过程   安卓中有三种对XML解析的方式,这个众所周知,DOM,SAX,PULL 其中被推荐的方法是PULL,说是非常简单,但从一开始接触就觉得比较迷惑,总是云里雾里的感觉,甚至在自己写出了一个能 ...

  9. csv参数化,数据驱动

    首先我们要有一个接口测试用例存放的地方,我们这里用EXCEL模板管理,里面包含用例编号.入参.优先级.请求方式.url等等. 1:新建一个txt文件,命名为sjqd,后缀名改为csv,右键excel格 ...

  10. sql server维护解决方案(备份、检查完整性、索引碎片整理)

    请务必看原文 原文:https://ola.hallengren.com/frequently-asked-questions.html 经常问的问题 入门 如何开始使用SQL Server维护解决方 ...