android - 在运行时在已经膨胀的层次结构中添加 View /布局?

标签 android performance android-layout android-fragments

我想知道在运行时将 View 添加到 View 层次结构到已经膨胀的 View 中是否是一种不好的做法。
让我们考虑一下我的例子。我有以下 Activity 类层次结构

  1. 基本 Activity - 一些基本 Activity ,包括生命周期方法的基本配置,没有膨胀布局(setContentView)。
  2. SingleFramgentActivity 它在 onCreate 方法中有 setContenview 方法。此 Activity 的布局如下

c

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout android:id="@+id/fragment_container"
                 xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:orientation="vertical">

    </FrameLayout
    <ViewStub
        android:id="@+id/stub_progress_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inflatedId="@+id/progress_bar_buttons"
        android:layout="@layout/inflate_stub_progressbar_bg"/>
</FrameLayout>  

这里有几个问题。
正如您在这里看到的,有一个 fragment 容器 FrameLayout 用于保存当前 fragment ,只要它是 SingleFragmentActivity

我需要做的是将工具栏添加到我的布局中,并且应该出现在该 Activity 中的所有 fragment 上。
在这种情况下,我的布局必须看起来像

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout android:id="@+id/fragment_container"
                 xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:orientation="vertical">

    </FrameLayout>
    <include
        android:id="@+id/toolbar"
        layout="@layout/include_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"/>
    <ViewStub
        android:id="@+id/stub_progress_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inflatedId="@+id/progress_bar_buttons"
        android:layout="@layout/inflate_stub_progressbar_bg"/>
</FrameLayout>  

这里有几个问题。
1. 我已经在我的 SingleFramgentActivity 类中扩充了布局。所以我只能添加到已经膨胀的层次结构,获取 Root View 并调用 addView 方法。如果是,请看下一个问题。
2.我只需要添加一些 View ,我需要添加布局文件来查看两个已经存在的 View 之间的层次结构。 FrameLayout 和 ViewStub 因为 View 在 FrameLayout 中的 Z 放置。

这个问题有几种解决方案
1. 在我需要的每个 fragment 中包含工具栏(在 Activity 内的所有 fragment 中)这似乎不是最好的方法,因为代码重复。 (不要重复自己的规则)。
2. 使用一些基本的 ToolbarFragment 类,在这种情况下,我必须在运行时通过继承的 fragment 添加所有需要的 View 。
3. 使用一些 Decorator 类 ToolbarActivity 并如上所述添加到已经膨胀的 View 层次结构中。 4. 使用与 SingleFramgentActivity 相同的布局,但将工具栏放在那里包括标签并将类重命名为 SingleToolbarFragmentActivity。似乎单一职责原则刚刚被打破。如果只需要没有 fragment 容器的工具栏,反之亦然。

所以我需要建议什么是最好的方法。是不是关于性能的坏习惯?
请建议在这种情况下使用什么更好,不会导致性能问题并尽可能保持代码的可读性

最佳答案

如果您不一个一个地添加它们,或者改变它的布局边界,或者相同的是,多次调用 requestLayout/invalidate,那么在运行时添加 View 没有性能问题。除此以外的任何事情都不会导致任何性能问题。

鉴于此,对于您的情况,我通常做的是创建一个我们称之为 BaseActivity 的方法,其中包含一些可以扩展的方法,例如 hasToolbar():boolean 并且在创建此 Activity 的那一刻,我膨胀了所需的内容并将其添加到 View 中。这是一种方法,但是您提到的任何方法都可以在没有性能问题的情况下工作,因为扩充单个布局并不那么耗时(例如在 ListView 中发生的情况)。

关于android - 在运行时在已经膨胀的层次结构中添加 View /布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31304624/

相关文章:

Linux 上的 Android Studio 2.3(及更早版本)无法加载项目 - Gradle 同步失败 : Index: 0, 大小:0

c++ - 将 uint64_t 位掩码转换为 std::array of bool

c++ - 了解内存池

java - 自动回复广播接收器收到的消息

android - 如何在另一个自定义 View 中添加自定义 View ?

android - 无法在 Azure VM 上使用 WHPX 运行 Android 模拟器

java - Android中字符串下划线修改

android - Android开发:AsyncTask中的progressdialog,但应用程序仍然崩溃

javascript - 函数包装器在类上的性能基准测试与仅构造函数调用调用和 HaveSameMap 的对比

android - com.android.support :design:24. 2.1 中的 float 操作按钮错误