Android查缺補漏(View篇)--布局文件中的“@+id”和“@id”有什麽區別?
阿新 • • 發佈:2018-01-29
新增 布局 parent 直接 使用 margin 移除 控件 Coding
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”有什麽區別?