• 首页
  • 狐文
  • 狐图
  • About
狐窝
OvO
  1. 首页
  2. Android
  3. 正文

Android 为View分配id

2022年10月20日 86点热度 8人点赞 0条评论

简单来说,就是xml的id不允许相同,而使用代码分配的id允许相同。在查找时会使用深度优先探索,分配的id是有优先级的

资料来自:https://stackoverflow.com/questions/8460680/how-can-i-assign-an-id-to-a-view-programmatically

Android id overview
An Android id is an integer commonly used to identify views; this id can be assigned via XML (when possible) and via code (programmatically.) The id is most useful for getting references for XML-defined Views generated by an Inflater (such as by using setContentView.)

Assign id via XML
Add an attribute of android:id="@+id/somename" to your view.
When your application is built, the android:id will be assigned a unique int for use in code.
Reference your android:id's int value in code using "R.id.somename" (effectively a constant.)
this int can change from build to build so never copy an id from gen/package.name/R.java, just use "R.id.somename".
(Also, an id assigned to a Preference in XML is not used when the Preference generates its View.)
Assign id via code (programmatically)
Manually set ids using someView.setId(int);
The int must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.)
For example, if creating and numbering several views representing items, you could use their item number.
Uniqueness of ids
XML-assigned ids will be unique.
Code-assigned ids do not have to be unique
Code-assigned ids can (theoretically) conflict with XML-assigned ids.
These conflicting ids won't matter if queried correctly (keep reading).
When (and why) conflicting ids don't matter
findViewById(int) will iterate depth-first recursively through the view hierarchy from the View you specify and return the first View it finds with a matching id.
As long as there are no code-assigned ids assigned before an XML-defined id in the hierarchy, findViewById(R.id.somename) will always return the XML-defined View so id'd.
Dynamically Creating Views and Assigning IDs
In layout XML, define an empty ViewGroup with id.
Such as a LinearLayout with android:id="@+id/placeholder".
Use code to populate the placeholder ViewGroup with Views.
If you need or want, assign any ids that are convenient to each view.
Query these child views using placeholder.findViewById(convenientInt);

API 17 introduced View.generateViewId() which allows you to generate a unique ID.

If you choose to keep references to your views around, be sure to instantiate them with getApplicationContext() and be sure to set each reference to null in onDestroy. Apparently leaking the Activity (hanging onto it after is is destroyed) is wasteful.. :)

Reserve an XML android:id for use in code
API 17 introduced View.generateViewId() which generates a unique ID. (Thanks to take-chances-make-changes for pointing this out.)*

If your ViewGroup cannot be defined via XML (or you don't want it to be) you can reserve the id via XML to ensure it remains unique:

Here, values/ids.xml defines a custom id:

<?xml version="1.0" encoding="utf-8"?>



Then once the ViewGroup or View has been created, you can attach the custom id

myViewGroup.setId(R.id.reservedNamedId);
Conflicting id example
For clarity by way of obfuscating example, lets examine what happens when there is an id conflict behind the scenes.

layout/mylayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

To simulate a conflict, lets say our latest build assigned R.id.placeholder(@+id/placeholder) an int value of 12..

Next, MyActivity.java defines some adds views programmatically (via code):

int placeholderId = R.id.placeholder; // placeholderId==12
// returns placeholder which has id==12:
ViewGroup placeholder = (ViewGroup)this.findViewById(placeholderId);
for (int i=0; i<20; i++){
TextView tv = new TextView(this.getApplicationContext());
// One new TextView will also be assigned an id==12:
tv.setId(i);
placeholder.addView(tv);
}
So placeholder and one of our new TextViews both have an id of 12! But this isn't really a problem if we query placeholder's child views:

// Will return a generated TextView:
placeholder.findViewById(12);

// Whereas this will return the ViewGroup placeholder;
// as long as its R.id remains 12:
Activity.this.findViewById(12);
*Not so bad

标签: android 为view分配id
最后更新:2022年10月20日

OvO

狐狸

点赞
< 上一篇
下一篇 >
最新 热点 随机
最新 热点 随机
EFI Shell 命令参考命令 说明 证书的SCT相关 ssl证书生成时插入OID TrueNAS SCALE docker安装使用联合文件系统mergerfs Iptables 端口(流量)转发 如何在GIT中删除第一个提交 TrueNas 虚拟机无法连接主机 dnsmasq ipset iptables 实现对流量进行分流 新版 Ubuntu 修改IP DCHP K3S 创建portainer 通过声音进行数据传输 Windows精简部署相关简易内容 ConstraintLayout 和 Coordinator 对比 Android 为View分配id wireshark解析TCP tcpdump rpcapd 相关编译方法 双网卡调整优先级 Android 禁用IPv6
EFI Shell 命令参考命令 说明
TrueNAS SCALE docker安装使用联合文件系统mergerfs Selenium Chrome浏览器的启动以及proxy设置 Ubuntu编译运行ss-redir打造透明代理Wifi环境 Python以表格的形式打印占用内存Top10的程序列表 tmux:适用于重度命令行 Linux 用户的终端复用器 Git remote 修改源 SQL Server中利用正则表达式替换字符串 钛备份或小米系统备份功能备份出来的APK无法正常还原 删除Maven仓库无用的版本 Comparison of proxifiers | 代理工具对比 CentOS 7 Shadowsocks优化 Git 设置网络代理 git 设置代理 验证码破解技术四部曲之使用卷积神经网络(四) Linux Deploy上安装MYSQL Debian通过PPA源安装软件 Maven:Generating Project in Batch mode 卡住问题 利用 Python + Selenium 实现对页面的指定元素截图(可截长图元素)
标签聚合
docker http android chrome ssl 未分类 linux 编译 windows ipv com 下载 网卡 git 密码 https

COPYRIGHT © 2020 狐窝. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS