可以多个片段共享一个单独的 XML 布局。

huangapple go评论83阅读模式
英文:

Can multiple fragments share a single XML layout

问题

我有多个共享相同布局的片段,其中包含一个放置在LinearLayout内部的RecyclerView。过去,我有如下文件:

fragment_one.xml
fragment_two.xml
fragment_three.xml
fragment_four.xml

不过,我目前在考虑是否更好地使用一个名为fragment_generic的单一布局。这是一个好主意还是坏主意?

谢谢。

英文:

I have multiple fragments that share the same layout which is a RecyclerView inside a LinearLayout. In the past, I had files like:

fragment_one.xml
fragment_two.xml
fragment_three.xml
fragment_four.xml

Though, I am currently questioning if it is better, to use a single layout named, for example, fragment_generic. Is this a good or bad idea?

Thanks.

答案1

得分: 3

这实际上取决于您的用途。如果您有大约5个片段,它们具有相同的布局,即单个RecyclerView列表。最好为所有目的使用单个布局。这会使应用程序变得非常紧凑。

稍后,当出现一些更改并且您想要添加项目,比如说一个浮动操作按钮图标,那么创建一个新的布局,并仅将其用于已更改的片段。

所以关于您的问题,是的,为这4个片段使用相同的布局将是最佳选择。

英文:

It actually depends on your use case. If you have like 5 fragments which have the same layout i.e a single recyclerview list. It is better to use a single layout for all the purposes. That makes the application really compact.

Later, when some changes come into place and you want to add item , let's suppose a fab icon, then create a new layout and use it for the changed fragment only.

So about your question, yes it would be the best idea to use the same layout for these 4 fragments.

huangapple
  • 本文由 发表于 2020年5月2日 19:15:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61558465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定