Android Studio在一个xml文件中更改ID会在另一个文件中更改ID

xin*_*aiz 8 refactoring android rename view android-studio

我曾经给Android Views提供简单的ID,但最近由于这个问题,我完全停止了该操作。

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.

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?

小智 4

不久前我也遇到了同样的问题,我发现这是 Android Studio 的预期行为,因为其他元素可能会受到 ID 发生更改的元素的限制。因此,您需要通过其文本编辑器更新所有 ID,特别是在新布局文件中,而不是从其设计编辑器的属性部分 ,它对我来说工作得很好,所以也对您来说也能很好地工作:)