Android中 @id 与 @+id 区别
Android 中的组件需要用一个int
类型的值来表示,这个值也就是组件标签中的id
属性值。id
属性只能接受资源类型的值,也就是必须以 @
开头的值,例如,@id/abc
、@+id/xyz
等。
如果在@
后面使用+
,表示当修改完某个布局文件并保存后,系统会自动在R.java
文件中生成相应的 int
类型变量。变量名就是 /
后面的值,例如,@+id/xyz
会在 R.java
文件中生成int xyz = value
,其中value
是一个十六进制
的数。如果 xyz
在R.java
中已经存在同名的变量,就不再生成新的变量,而该组件会使用这个已存在的变量的值。
既然组件的 id
属性是一个资源id
就可以,那么自然可以设置任何已经存在的资源id
值,例如,@drawable/icon
、@string/ok
、@+string/you
。当然,也可以设置 android 系统中已存在的资源 id,例如,楼主提出的 @id/android:list
,那么这个 android
是什么意思呢,实际上,这个 android 就是系统的R 类
(在R.java文件中)所在的 package
。我们可以在 Java 代码编辑区输入android.R.id.
,就会列出相应的资源 id
,例如,也可以设置id属性值为@id/android:message
。
<ListView android:id="@+id/android:message" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
还有另外一种方法查看系统中定义的 id
,
进入
<?xml version="1.0" encoding="utf-8"?><resources> <item type="id"name="price_edit">falseitem> <item type="id"name="amount_edit">falseitem>resources>
若在 ids.xml
中定义了Id
,则在layout
中可如下定义@id/price_edit
,否则@+id/price_edit
即是说当我们第一次定义一个控件的 id 时,我们需要使用 @+id/xxx,而当我们使用这个 id 时可以使用 @id/xx 的形式,如下可以看到 iv_userhead
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp"> <ImageView android:id="@+id/iv_userhead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:focusable="false" android:src="@drawable/ic_chat_head"/> <TextView android:id="@+id/tv_chatcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_centerVertical="true" android:layout_toEndOf="@id/iv_userhead" android:padding="10dp" android:background="@drawable/sh_chat_bg_msg_from" android:textSize="14sp"/> RelativeLayout>
更多相关文章
- android中,实现水平方向上三个按钮左对齐、居中对齐、右对齐效果
- Android属性动画、函数动画
- android中focusable和focusableInTouchMode
- 关于Android中的Intent一点说明
- 高效android编程
- 34、Android编写应用-从模板添加代码
- Android模拟开关按钮点击打开动画(属性动画之平移动画)
- 安居客Android项目架构演进
- tools:text和android:text