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

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

问题描述

我曾经给Android Views提供简单的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天全站免登陆