假设您有一个 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>
因此:
- 更改
LinearLayout
可见性,以显示另一个项目,将是一个巨大的性能问题 ? - 既然这是一个问题,为什么使用
ViewFlipper
不会降低应用性能?
最佳答案
这是不好的做法,因为代码很容易变得一团糟。忽略这一点并只关注性能,当您将可见性设置为 GONE
时,不会测量 View (它不同于 INVISIBLE
)。不过,该 View 占用了一点内存。根据您的操作,考虑使用 ViewGroup.removeView()
。
没有基准很难说,但理论上它不应该有性能问题。
关于android - 更改太多布局的可见性可能会导致性能问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44808212/