码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-01-29 00:19:17      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:编译错误   错误信息   xmlns   引用   1.0   就是   报错   工程   cti   

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”有什么区别?

标签:编译错误   错误信息   xmlns   引用   1.0   就是   报错   工程   cti   

原文地址:https://www.cnblogs.com/codingblock/p/8372836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!