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

时间:2022-05-06
本文章向大家介绍Android查缺补漏(View篇)--布局文件中的“@+id”和“@id”有什么区别?,主要内容包括Android布局文件中的“@+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>

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