【发布时间】:2016-06-09 07:11:10
【问题描述】:
我有一个 Android 应用程序,其中几乎所有活动都有侧边菜单。为此,我必须在我的活动 xml 中添加一个抽屉布局和一些其他项目,以获得一致的 UI。 例如
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar"
android:id="@+id/toolbar_container"/>
<<--ACTIVITY_XML-->>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
有什么办法可以不一次又一次地复制代码。
我想到了一种方法,将<<--ACTIVITY_XML-->> 替换为包含标签。然后我会在我的基本活动中覆盖setContentView(),我会在其中膨胀上述基本布局,然后在其中膨胀包含标签。这种方式在内存和时间利用方面是好是坏。
【问题讨论】:
-
搜索包含和合并标签以实现更好的优化
-
您可以将 1 个 Activity 用作带有常用 UI 元素和抽屉的主容器,然后在该 Activity 中使用 replace Fragment,这是推荐的方法。
标签: android android-layout code-reuse