ids概述
ids.xml:为应用的相关资源提供唯一的资源id。id是为了获得xml中的对象需要的参数,也就是 Object = findViewById(R.id.id_name); 中的id_name。
使用
例如xml中定义如下
<resources>
<item type="id" name="btn_next" />
</resources>
- xml中使用id
<Button
android:id="@id/btn_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="next"/>
- java使用id
Button button = new Button(this);
button.setId(R.id.btn_next);
优点
- 命名方便,我们可以把一些特定的控件先命好名,在使用的时候直接引用id即可,省去了一个命名环节。
- 优化编译效率
添加id后会在R.java中生成
使用ids.xml统一管理,一次性编译即可多次使用.public static final class id { ... public static final int bottom=0x7f040001; public static final int btn_next=0x7f040002; ...
但使用"@+id/btn_next"的形式,每次文件保存(Ctrl+s)后R.java都会重新检测,如果存在该id则不生成,如果不存在就需要添加该id,故编译效率降低.