我目前正在学习一种活动+多个片段的方法,这是Google推荐的。我想在片段之间共享一些布局:工具栏,滚动条,应用栏。
做到这一点的最佳方法是什么?
例如GitHub中的此应用程序: https://github.com/mitchtabian/Open-API-Android-App
答案 0 :(得分:0)
对于要重用的每个布局,您都需要有一个单独的布局文件。然后,您可以将它们包括在片段的布局中-通过<include>
标签来实现。
工具栏的示例布局(toolbar.xml):
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
重用此示例:
<include layout="@layout/toolbar"/>
关于如何实现此here的文档很好。