zoukankan      html  css  js  c++  java
  • 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>
    

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

  • 相关阅读:
    前端性能优化--图片处理(Css Sprites 与 base64)
    CSS规范--春风十里不如写好CSS
    前端性能优化--图片懒加载(lazyload image)
    struts2不同版本在核心filter在web.xml中的配置
    Web.xml配置详解之context-param
    web.xml的加载过程是context-param >> listener >> fileter >> servlet
    web.xml中Listener的作用
    web.xml中Filter的作用
    字符串反转
    fastjson是什么东东?
  • 原文地址:https://www.cnblogs.com/codingblock/p/8372836.html
Copyright © 2011-2022 走看看