在Android中命名XML元素id的最佳实践是什么?

时间:2022-11-28 08:45:19

I am not sure if anyone was bugged with this issue before, but it is one big pain for me.

我不知道以前是否有人被这个问题困扰过,但这对我来说是一个很大的痛苦。

How do you give an id to xml element in android?

如何在android中为xml元素提供id?

Right now, I set the id with the pattern [activity/fragment name][element type][specific name]. For example, if I had an EditText to keep an email which is used in LoginActivity, I will give the id 'LoginEditTextEmail'.

现在,我使用模式[activity/fragment name][element type][specific name]设置id。例如,如果我有一个EditText来保存LoginActivity中使用的电子邮件,我将给出id 'LoginEditTextEmail'。

The problem I'm facing is, with this approach, the id often ends up in a very long one.

我面临的问题是,使用这种方法,id通常会以一个很长的id结束。

How do you set the id? What is the best practice?

如何设置id?最佳实践是什么?

4 个解决方案

#1


12  

Descriptive names are ideal (same as with the name to any variable in any programming language).

描述性名称是理想的(与任何编程语言中任何变量的名称相同)。

I think you have a good system already I would offer these potential ways to decrease the size of your IDs

我认为你已经有了一个很好的系统我将提供这些潜在的方法来减少你的id的大小

[activity/fragment name] - Personally I would drop this, I tend to use one layout file per activity / fragment anyhow so there is no confusing what activity the view is meant to be in. Also there are times when I re-use some View widgets in multiple activities and I will leave them with the same ID so that the code to find and interact them is simplified (i.e. it can be copy/paste or put into a subclass of Activity)

[activity/fragment name]——就我个人而言,我将删除这个,我倾向于为每个activity/fragment使用一个布局文件,这样就不会让人混淆这个视图应该包含哪些活动。还有一些时候,我在多个活动中重用一些视图小部件,我将它们保留相同的ID,以便查找和交互它们的代码得到简化(例如,可以复制/粘贴或放入活动的子类)

[element type] - I use a 3 letter shorthand for the widget types:

[元素类型]-我对小部件类型使用3个字母的简写:

  • Edt = EditText
  • 美国东部时间= EditText
  • Txt = TextView
  • Txt = TextView
  • Lbl = TextView that is static for labeling something
  • 是一个静态的标签
  • Btn = Button
  • Btn =按钮
  • Prg = ProgressBar
  • Prg = ProgressBar
  • Lyt = Layout
  • Lyt =布局
  • etc...
  • 等等……

[specific name] - no real improvement to be made here, it has to be as long as it has to be to describe what it is for.

[具体名称]-这里没有真正的改进,只要描述它的用途就可以。

#2


5  

You're overcomplicating things. Just name it in whatever way is memorable to you. IDs only have to be unique per XML (i.e. you can have 50 different layouts with the id of my_edittext) since you find a view by it's ID only through a single view hierarchy.

你过于复杂的事情。只要你能记住它的名字就行。id对于每个XML都必须是唯一的(例如,您可以使用my_edittext的id拥有50个不同的布局),因为您只能通过一个视图层次结构通过它的id找到一个视图。

#3


1  

Much like naming anything, I tend to use the shortest name possible that accurately describes it. In the case of ids for layouts, just make sure each id is unique in your layout (you can reuse the same id in a different layout).

就像命名任何东西一样,我倾向于使用尽可能短的名字来准确地描述它。对于布局的id,只需确保布局中的每个id都是惟一的(您可以在不同的布局中重用相同的id)。

#4


0  

Drop fragment, activity and type unless you qualify these in coding, so:

删除片段、活动和类型,除非您在编码中符合这些条件,所以:

boolean isLoggedIn = false;

布尔isLoggedIn = false;

android:id="@+id/is_logged_in"

android:id =“@ + id / is_logged_in”

As mentioned in other responses the XML provides the qualifier, now you have to decide whether consistency and/or further qualification is necessary and if it befits. Do you really need to qualify and if you don't is supporting the code going to be a lot harder by you or anyone else?

正如在其他响应中提到的,XML提供了限定符,现在您必须决定是否需要一致性和/或进一步的限定以及它是否适合。你真的需要限定条件吗?如果你不支持代码,那么你或其他人就会很难支持代码吗?

So what about strings.xml?

那么strings.xml呢?

prefix the id with a frag or activity qualifier. So, for example:

在id前面加上frag或activity限定符。举个例子:

<string name="profile_is_logged_in_true">Logged in.</string>

<字符串名称= " profile_is_logged_in_true> 登录。< /字符串>

<string name="profile_is_logged_in_false">Not logged in.%</string>

<字符串名称= " profile_is_logged_in_false> 不登录英寸% > < /字符串

Also…

也…

<plurals name="plural_is_logged_in_duration">...

<复数name = " plural_is_logged_in_duration> …

I've toyed with:

我玩:

<string name="profile_isloggedin_false">Not logged in.</string>

<字符串名称= " profile_isloggedin_false> 没有登录。< /字符串>

But not yet convinced. This is actually a classification problem, now since solved in other disciplines by tagging. Java provides dotted namespace and type qualifiers, so

但没有信服。这实际上是一个分类问题,现在已经通过标记在其他学科中解决了。Java提供了虚线命名空间和类型限定符

com.example.android.app.profile.State.isLoggedIn

com.example.android.app.profile.State.isLoggedIn

Android uses things like R.string :-) So you have the Java plus some additional namespace from Android. Don't forget you can have more than one string resources file, so perhaps:

Android使用R之类的东西。-)所以你有Java和一些来自Android的附加名称空间。不要忘记您可以有多个字符串资源文件,所以可能:

res/values/widget_defaults.xml

res /价值/ widget_defaults.xml

could contain some default values for say a TextView. The true and false strings should be handled by plurals - but the example hopefully helps, despite a tad contrived.

可以包含一些默认值,比如TextView。真字串和假字串应该由复数来处理——但是这个例子希望能有所帮助,尽管有点做作。

#1


12  

Descriptive names are ideal (same as with the name to any variable in any programming language).

描述性名称是理想的(与任何编程语言中任何变量的名称相同)。

I think you have a good system already I would offer these potential ways to decrease the size of your IDs

我认为你已经有了一个很好的系统我将提供这些潜在的方法来减少你的id的大小

[activity/fragment name] - Personally I would drop this, I tend to use one layout file per activity / fragment anyhow so there is no confusing what activity the view is meant to be in. Also there are times when I re-use some View widgets in multiple activities and I will leave them with the same ID so that the code to find and interact them is simplified (i.e. it can be copy/paste or put into a subclass of Activity)

[activity/fragment name]——就我个人而言,我将删除这个,我倾向于为每个activity/fragment使用一个布局文件,这样就不会让人混淆这个视图应该包含哪些活动。还有一些时候,我在多个活动中重用一些视图小部件,我将它们保留相同的ID,以便查找和交互它们的代码得到简化(例如,可以复制/粘贴或放入活动的子类)

[element type] - I use a 3 letter shorthand for the widget types:

[元素类型]-我对小部件类型使用3个字母的简写:

  • Edt = EditText
  • 美国东部时间= EditText
  • Txt = TextView
  • Txt = TextView
  • Lbl = TextView that is static for labeling something
  • 是一个静态的标签
  • Btn = Button
  • Btn =按钮
  • Prg = ProgressBar
  • Prg = ProgressBar
  • Lyt = Layout
  • Lyt =布局
  • etc...
  • 等等……

[specific name] - no real improvement to be made here, it has to be as long as it has to be to describe what it is for.

[具体名称]-这里没有真正的改进,只要描述它的用途就可以。

#2


5  

You're overcomplicating things. Just name it in whatever way is memorable to you. IDs only have to be unique per XML (i.e. you can have 50 different layouts with the id of my_edittext) since you find a view by it's ID only through a single view hierarchy.

你过于复杂的事情。只要你能记住它的名字就行。id对于每个XML都必须是唯一的(例如,您可以使用my_edittext的id拥有50个不同的布局),因为您只能通过一个视图层次结构通过它的id找到一个视图。

#3


1  

Much like naming anything, I tend to use the shortest name possible that accurately describes it. In the case of ids for layouts, just make sure each id is unique in your layout (you can reuse the same id in a different layout).

就像命名任何东西一样,我倾向于使用尽可能短的名字来准确地描述它。对于布局的id,只需确保布局中的每个id都是惟一的(您可以在不同的布局中重用相同的id)。

#4


0  

Drop fragment, activity and type unless you qualify these in coding, so:

删除片段、活动和类型,除非您在编码中符合这些条件,所以:

boolean isLoggedIn = false;

布尔isLoggedIn = false;

android:id="@+id/is_logged_in"

android:id =“@ + id / is_logged_in”

As mentioned in other responses the XML provides the qualifier, now you have to decide whether consistency and/or further qualification is necessary and if it befits. Do you really need to qualify and if you don't is supporting the code going to be a lot harder by you or anyone else?

正如在其他响应中提到的,XML提供了限定符,现在您必须决定是否需要一致性和/或进一步的限定以及它是否适合。你真的需要限定条件吗?如果你不支持代码,那么你或其他人就会很难支持代码吗?

So what about strings.xml?

那么strings.xml呢?

prefix the id with a frag or activity qualifier. So, for example:

在id前面加上frag或activity限定符。举个例子:

<string name="profile_is_logged_in_true">Logged in.</string>

<字符串名称= " profile_is_logged_in_true> 登录。< /字符串>

<string name="profile_is_logged_in_false">Not logged in.%</string>

<字符串名称= " profile_is_logged_in_false> 不登录英寸% > < /字符串

Also…

也…

<plurals name="plural_is_logged_in_duration">...

<复数name = " plural_is_logged_in_duration> …

I've toyed with:

我玩:

<string name="profile_isloggedin_false">Not logged in.</string>

<字符串名称= " profile_isloggedin_false> 没有登录。< /字符串>

But not yet convinced. This is actually a classification problem, now since solved in other disciplines by tagging. Java provides dotted namespace and type qualifiers, so

但没有信服。这实际上是一个分类问题,现在已经通过标记在其他学科中解决了。Java提供了虚线命名空间和类型限定符

com.example.android.app.profile.State.isLoggedIn

com.example.android.app.profile.State.isLoggedIn

Android uses things like R.string :-) So you have the Java plus some additional namespace from Android. Don't forget you can have more than one string resources file, so perhaps:

Android使用R之类的东西。-)所以你有Java和一些来自Android的附加名称空间。不要忘记您可以有多个字符串资源文件,所以可能:

res/values/widget_defaults.xml

res /价值/ widget_defaults.xml

could contain some default values for say a TextView. The true and false strings should be handled by plurals - but the example hopefully helps, despite a tad contrived.

可以包含一些默认值,比如TextView。真字串和假字串应该由复数来处理——但是这个例子希望能有所帮助,尽管有点做作。