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

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

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

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

示例1 正确的使用:

<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world"/>

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

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

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

<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设置,不会报错但是无意义。

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

使用示例:

(1)创建ids.xml

<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

<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的控件。

时间: 2024-08-06 09:09:54

Android----xml文件中的控件的id设置的相关文章

Android在onCreate()中获得控件尺寸

@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        final ImageView imageView = (ImageView) findViewById(R.id.imageview);              int w = View.Mea

wpf 中DataGrid 控件的样式设置及使用

本次要实现的效果为: 这个DataGrid需要绑定一个集合对象,所以要先定义一个Experience类,包含三个字段 /// <summary> /// 定义工作经历类 /// </summary> public class Experience { /// <summary> /// 获取或设置工作的起始时间 /// </summary> public string Start { get; set; } /// <summary> /// 获

Android之activity中新建控件

了解了5大布局,我们会发现这些布局都是静态的,如何让系统自动生成控件呢?这就需要activity来帮忙了 今天我们讲的就是用activity新建布局 用案例来说吧! 实现一个输入行和列自动生成表格并生成背景颜色 效果如图 代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

Android在OnCreate中获取控件的宽度和高度

原文链接:http://www.cnblogs.com/wt616/archive/2012/05/11/2496180.html 在Android中,有时需要对控件进行测量,得到的控件宽度和高度可以用来做一些计算.在需要自适应屏幕的情况下,这种计算就显得特别重要.另一方便,由于需求的原因,希望一进入界面后,就能得到控件的宽度和高度. 可惜的是,根据我的验证,利用网上转载的那些方法在OnCreate函数中获取到的仍然是0(希望搞技术的能自己验证过再转载),例如Measure方法之后调用getMe

Android在onCreate中获取控件的宽高

在某些需求下,我们需要在onCreate的时候就获取到控件的宽高,但是如果直接用view.getWidth()或view.getHeight()会得到0.这是因为在onCreate执行的时候,控件还没有被绘制出来. 利用下面的方法可以获得控件的宽高: ViewTreeObserver vto = zoomImageView.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(

winform中DataGrid控件的宽度设置

最近修改一个win5.0的PDA程式,碰到一个问题.就是给DataGrid控件绑定数据的时候,这个控件的宽度不能调整,有时候数据较长,就显示不全.然后想在程式里自定义它的宽度,设置不成功.然后网上没找到合适的方法去设置它的宽度.最后从同事LYL那里得到解决方法 设置数据源: this.dataGrid1.DataSource = ds.Tables[1].DefaultView; 在这之前给DataGrid控件添加表样式,代码如下: DataGridTableStyle h = new Data

【android】在xml文件中定义drawable数组、id数组等

假如我们需要在代码中为一组view设置相对应的一组图片资源时(如为listview/gridview的所有item设置一组对应的图片时),我们就可以在xml中定义一组代表图片的drawable数组,然后用代码进行读取. 开始我是这样做的: <integer-array name="actions_images"> <item>@drawable/pencil1</item> <item>@drawable/pencil2</item

从smack-config.xml文件中加载文件内容 Loads the configuration from the smack-config.xml file

/** * Loads the configuration from the smack-config.xml file.<p> * * So far this means that: * 1) a set of classes will be loaded in order to execute their static init block * 2) retrieve and set the current Smack release */ static { try { // Get an

android开发中一个activity如何调用另一个xml中的控件

有时候,我们需要在一个Activity中调用另一个xml布局文件,即非本Activity所绑定的xml布局文件中的控件,这时候就不能直接findViewById,不然会报错指向空对象,这时就需要像下面这样做. LayoutInflater factory = LayoutInflater.from(当前类.this); View layout = factory.inflate(R.layout.你要获取的另一个XML, null); TextView textview = (TextView)