Android----xml文件中的控件的id设置

简介:

    Android开放中要想得到布局文件中控件的引用,该控件必须设置id属性,这两有两种方式设置id:(1)@+id/xxxx;(2)@id/xxxx;下面做个简单的介绍。

  1. @+id/xxx:如果R文件中没有该id则创建;

注意:一个xml文件中不能出现两个以该形式设置同一id的两个控件(include标签例外);

示例1 正确的使用:

1
2
3
4
5
< TextView
     android:id = "@+id/mytv"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "hello world" />

示例2 错误(两个id相同):此时系统会提醒报错

1
2
3
4
5
6
7
8
9
10
< TextView
     android:id = "@+id/mytv"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "hello world" />
< TextView
     android:id = "@+id/mytv"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "hello world" />

示例3 允许以下用法,但是该id指向的是include标签,之后的linearLayout设置id的操作无意义:

1
2
3
4
5
6
7
8
9
10
< include
     android:id = "@+id/include1"
     layout = "@layout/my"
     android:layout_width = "50dp"
     android:layout_height = "50dp" />
< LinearLayout
     android:id = "@+id/include1"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:orientation = "horizontal" />

如果将include标签与LinearLayout交换位置则会报错。

示例 4 允许以下用法,但是该id指向TextView,之后的include标签和LinearLayout设置id无意义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<TextView
     android:id= "@+id/mytv"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:text= "hello world" />
<include
     android:id= "@id/mytv"
     layout= "@layout/my"
     android:layout_width= "50dp"
     android:layout_height= "50dp" />
<LinearLayout
     android:id= "@id/mytv"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:orientation= "horizontal" />

如果将TextView的位置下移,运行会出错。如果include中引用的布局存在与TextView相同的id设置,不会报错但是无意义。

2.@id/xxxx:引用ids.xml中相应的id,与@+id/xxx不同,一旦向ids.xml文件中添加一个id在R.java文件中会生成一个相应的id,无论是否有控件使用该id。

使用示例:

(1)创建ids.xml

1
2
3
4
5
6
7
8
9
10
< resources >
     < item  name = "hello"  type = "id"  />
     < item  name = "hello2"  type = "id"  />
     < item  name = "hello3"  type = "id"  />
     < item  name = "hello4"  type = "id"  />
     < item  name = "hello5"  type = "id"  />
     < item  name = "hello6"  type = "id"  />
     < item  name = "hello7"  type = "id"  />
     < item  name = "hello8"  type = "id"  />
</ resources >

(2)使用id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< TextView
     android:id = "@id/hello"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "hello 1"  />
 
< TextView
     android:id = "@id/hello"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "hello 2"  />
 
< TextView
     android:id = "@id/hello"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "hello 3"  />

多个控件可以以同样的方式设置统一id,但是该id只属于最先使用该id的控件。











本文转自wauoen51CTO博客,原文链接: http://blog.51cto.com/7183397/1847317,如需转载请自行联系原作者


相关文章
|
4天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
【5月更文挑战第10天】BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
13 1
|
4天前
|
Android开发
android string.xml文件中的整型和string型代替
android string.xml文件中的整型和string型代替
|
4天前
|
XML 安全 API
AndroidManifest.xml文件综合详解
AndroidManifest.xml文件综合详解
|
5天前
|
XML Java Android开发
Android控件动态使用 (转)
Android控件动态使用 (转)
|
7天前
|
Android开发
android 12 U盘 /mnt/media_rw 下读取文件异常 没有权限
android 12 U盘 /mnt/media_rw 下读取文件异常 没有权限
18 0
|
7天前
|
XML Android开发 数据安全/隐私保护
android 11后文件读写访问权限申请
android 11后文件读写访问权限申请
13 0
|
7天前
|
JSON Android开发 数据格式
Android 打开系统文件管理器,并返回选中文件的路径
Android 打开系统文件管理器,并返回选中文件的路径
13 0
|
7天前
|
Java Shell Android开发
Android11 有线网和wifi优先级设置
Android11 有线网和wifi优先级设置
12 0
|
8天前
|
Java Android开发
Android 设置系统时区的源码追踪
Android 设置系统时区的源码追踪
11 1
|
16天前
|
XML 存储 Java
【OpenCV】—输入输出XML和YAML文件
【OpenCV】—输入输出XML和YAML文件