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"/>
1
2
3
4
还有另外一种方法查看系统中定义的 id,
进入
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id"name="price_edit">false</item>
<item type="id"name="amount_edit">false</item>
</resources>
1
2
3
4
5
若在 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>
---------------------
作者:玛斯特・布兰迪
来源:CSDN
原文:https://blog.csdn.net/strange_monkey/article/details/80844814
版权声明:本文为博主原创文章,转载请附上博文链接!