简体   繁体   English

多个片段能否共享一个 XML 布局

[英]Can multiple fragments share a single XML layout

I have multiple fragments that share the same layout which is a RecyclerView inside a LinearLayout .我有多个片段共享相同的布局,即LinearLayout内的RecyclerView In the past, I had files like:过去,我有如下文件:

fragment_one.xml fragment_two.xml fragment_three.xml fragment_four.xml 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 .不过,我目前正在质疑使用名为fragment_generic的单个布局是否更好。 Is this a good or bad idea?这是个好主意还是坏主意?

Thanks.谢谢。

It actually depends on your use case.这实际上取决于您的用例。 If you have like 5 fragments which have the same layout ie a single recyclerview list.如果您有 5 个具有相同布局的片段,即一个回收视图列表。 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.稍后,当一些更改发生并且您想要添加项目时,假设一个 fab 图标,然后创建一个新布局并将其仅用于更改的片段。

So about your question, yes it would be the best idea to use the same layout for these 4 fragments.所以关于你的问题,是的,最好对这 4 个片段使用相同的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM