Scope of R.id values

前端 未结 3 495
野趣味
野趣味 2021-01-01 17:26

I\'m looking for ways to reduce what I would call \"id pollution\" in my apps. From what I can tell, R.id is global to the application, so in every Activity I

相关标签:
3条回答
  • 2021-01-01 17:56

    You're fine to use the same id across multiple elements, as long as they aren't in the same view. So, all your save buttons could have the id of btn_save and as long as there aren't two of them in the same layout file, or attached layouts, then you're fine.

    0 讨论(0)
  • 2021-01-01 18:11

    I use:

    R.id.activityName_type_action

    where type may be [btn|txtview|edittext|listview...]

    and action is something like [save|del|accept|name|pin...]

    It's pretty verbose, but this way I can guess the identifier name from the activity without having to continuosly check the xml layout.

    For example:

    R.id.loginpin_btn_accept

    R.id.loginpin_txtview_pin

    0 讨论(0)
  • 2021-01-01 18:18

    The other answers will work, but you could also make a save_button.xml in your layout folder, with <Button> as the root tag. Then reference that guy's id in those places that you need it. This means you only need to define one "save" button, and use it everywhere.

    0 讨论(0)
提交回复
热议问题