Android Studio 更改一个 xml 文件中的 ID 会更改另一个文件中的 ID [英] Android Studio changing ID's in one xml file changes the ID's in another file

查看:24
本文介绍了Android Studio 更改一个 xml 文件中的 ID 会更改另一个文件中的 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经为 Android 视图提供简单的 id,但最近,由于这个问题,我完全停止了.

I used to give simple ids to Android Views, but lately, I stopped that altogether because of this issue.

假设我有 2 个布局,一个名为 first_layout.xml,第二个名为 second_layout.xml.在这些布局中的每一个中,都有一个 ID 为 username_input 的视图.这个 id 用于不同的活动类来访问目标视图,可以是 ButterKnife 绑定、Kotlin Android 扩展,甚至 findViewById.

Let's say I have 2 layouts, one named first_layout.xml and second named second_layout.xml. In each of these layouts, there is a view with id username_input. This id is used in different activity classes to access target view, be it ButterKnife binding, Kotlin Android Extensions, or even findViewById.

现在,无论出于何种原因,我只需要在一种布局中重命名 id.因此我做 Refactor ->重命名.发生什么了?Android Studio 只是简单地替换了整个项目中的 id username_input,弄乱了其他具有相同 id 的布局.大多数人不会为项目中的每个视图使用唯一 ID,因为它不是强制性的,但我这样做是因为我进行了大量重构,并且很多时候我因为这种非-而损失了小时项目中的上下文替换.

Now, for whatever reason, I need to rename id in just one layout. Thus I do Refactor -> Rename. What happens? Android Studio simply replaces id username_input in the whole project, messing other layouts that had view the same id. Most people don't use a unique id for each and every view in the project since it's not mandatory, but I do because I do lots of refactoring, and many times I lost hours because of this non-contextual replacement in the project.

在这种情况下我有哪些选择?在这种情况下,有没有办法让 Android Studio 进行上下文替换,或者我应该继续在我的项目中制作唯一的 id?

What are my options in this case? Is there a way to make Android Studio do the contextual replacement in such a situation, or should I keep making unique ids in my project?

推荐答案

不久前我遇到了同样的问题,我发现这是 Android Studio 的预期行为,因为其他元素可能会被限制到其 ID 正在获取的元素改变了.因此,您需要更新所有 ID,尤其是在新布局文件中通过其文本编辑器而不是从其设计编辑器的属性"部分它对我来说很好用,所以对你来说也很好用 :)

Moments ago I was facing this same issue and I found it to be the Android Studio's expected behavior, as other elements might be constrained to the element whose ID is getting changed. So you need to update all the IDs particularly in your new layout file through its text editor AND NOT from the Attribute section of its design editor Its working fine for me, and so must work well for you too :)

这篇关于Android Studio 更改一个 xml 文件中的 ID 会更改另一个文件中的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆