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,从细节上更进一步的讲解自定 ...
随机推荐
- pandas category数据类型
实际应用pandas过程中,经常会用到category数据类型,通常以string的形式显示,包括颜色(红,绿,蓝),尺寸的大小(大,中,小),还有地理信息等(国家,省份),这些数据的处理经常会有各种 ...
- [LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...
- MTD 移动目标防御技术
移动目标防御技术,主要包括系统随机化,生物启发MTD,网络随机化,云MTD,动态编译等等.研讨会还就威胁建模和量化移动目标防御技术的效能评估进行了推进.理论和定量的模型对于该技术的颠覆性影响至关重要. ...
- css3的一个小demo(箭头hover变化)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 2018/03/10 每日一个Linux命令 之 find
每日一个Linux命令 2018-03-10 Linux 命令 find find [查找目录] [定义条件] 今天很累了,本来不想写了,但想到自己订的学习计划必须坚持下去,每天完成. fin ...
- oracle行转列,列转行
多行转字符串这个比较简单,用||或concat函数可以实现 SQL Code select concat(id,username) str from app_userselect id||userna ...
- 高并发秒杀系统方案(集成Mybatis和Redis)
1.集成Mybatis 第一步,添加依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> < ...
- Stringbuffer扩容
public class A { public static void main(String[] args) { StringBuffer ab=new StringBuffer(); String ...
- 【虫师讲Selenium+Python】第三讲:操作测试对象
一.首先呢,选择一个编辑器,我们这里选择的是Sublime Text >Ctrl+B为运行当前脚本的快捷方式 二.编写代码 #coding==utf-8 from selenium import ...
- python-面向对象-10-单例
单例 目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对 某一特定问题 的成熟 ...