1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android的xml登录 Android----xml文件中的控件的id设置

android的xml登录 Android----xml文件中的控件的id设置

时间:2021-11-27 10:18:27

相关推荐

android的xml登录 Android----xml文件中的控件的id设置

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

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

示例1 正确的使用:

android:id="@+id/mytv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="helloworld"/>

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

android:id="@+id/mytv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="helloworld"/>

android:id="@+id/mytv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="helloworld"/>

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

android:id="@+id/include1"

layout="@layout/my"

android:layout_width="50dp"

android:layout_height="50dp"/>

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无意义:

android:id="@+id/mytv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="helloworld"/>

android:id="@id/mytv"

layout="@layout/my"

android:layout_width="50dp"

android:layout_height="50dp"/>

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

(2)使用id

android:id="@id/hello"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="hello1"/>

android:id="@id/hello"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="hello2"/>

android:id="@id/hello"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="hello3"/>

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。