android - 更改太多布局的可见性可能会导致性能问题吗?

标签 android performance android-layout leakcanary timber-android

假设您有一个 FrameLayout包含 10 LinearLayouts ,每次只能看到一个。

每个LinearLayout是一个复杂的 View ,包含 Button , EditText , TextView

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/alice
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <!-- complex stuff -->

    </LinearLayout>

    <!-- many more linear layouts... -->

    <LinearLayout
        android:id="@+id/juliett
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <!-- last complex stuff -->

    </LinearLayout>

</FrameLayout>

因此:

  1. 更改 LinearLayout可见性,以显示另一个项目,将是一个巨大的性能问题 ?
  2. 既然这是一个问题,为什么使用 ViewFlipper 不会降低应用性能?

最佳答案

这是不好的做法,因为代码很容易变得一团糟。忽略这一点并只关注性能,当您将可见性设置为 GONE 时,不会测量 View (它不同于 INVISIBLE)。不过,该 View 占用了一点内存。根据您的操作,考虑使用 ViewGroup.removeView()

没有基准很难说,但理论上它不应该有性能问题。

关于android - 更改太多布局的可见性可能会导致性能问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44808212/

相关文章:

c# - 在 Windows 中对程序进行基准测试的最佳方法是什么?

android - 如何将 ListView 按钮拉伸(stretch)到屏幕的全宽

android - 下面带有 MPAndroidChart(或其他)的 ListView

java - 如果数据库包含现有数据,如何仅在 Activity 中调用一次方法?

android - 避免按钮多次快速点击

java - Android 如何知道某个方法已被弃用?

android - 在 Android 上保存自定义 View 的屏幕截图

android - 如何从 Gallery 迁移到 Horizo​​ntalScrollView & ViewPager?

performance - 了解Elasticsearch慢查询日志格式

c# - IEnumerable 在使用后是否存储对象