前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android查缺补漏(View篇)--布局文件中的“@+id”和“@id”有什么区别?

Android查缺补漏(View篇)--布局文件中的“@+id”和“@id”有什么区别?

作者头像
codingblock
发布2018-03-30 11:11:25
1K0
发布2018-03-30 11:11:25
举报
文章被收录于专栏:CodingBlockCodingBlock

Android布局文件中的“@+id”和“@id”有什么区别?

  • +id表示为控件指定一个id(新增一个id),如:
代码语言:javascript
复制
<cn.codingblock.view.customer_view.MyView
        android:id="@+id/myview"
        ...
        />
  • id表示引用一个现有的id,如:
代码语言:javascript
复制
<cn.codingblock.view.customer_view.MyView
        android:id="@+id/myview"
        android:layout_below="@id/btn_handle_myview"
        .../>

但需要注意的是在布局文件中,被引用的id要在引用位置的上面,否则会编译出错,如下:

代码语言:javascript
复制
<?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>

编译错误信息:

代码语言:javascript
复制
Error:(14, 31) No resource found that matches the given name (at 'layout_below' with value '@id/btn_handle_myview').

解决方法:

  • 方法一:将引用id的位置改成+id,意思也就是说先将此id新增到工程的R文件中,如下:
代码语言:javascript
复制
<?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的下面位置,如下:
代码语言:javascript
复制
<?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>

这是一个小知识点,非常简单,但确是我们很容易忽略的一个地方,所以今天记录一下。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Android布局文件中的“@+id”和“@id”有什么区别?
  • 解决方法:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档