Sea*_*ean 12 performance android view android-fragments
在使用层次结构查看器来减少层次结构时,我注意到在每次添加片段时(以"静态"或"动态"方式),片段总是包含在新的FrameLayout中.
这是一个例子:
这是我的活动布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="mainActivityRoot" >
<TextView
android:id="@+id/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:name="com.example.testfragments.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/hello_world" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是片段布局:
<ProgressBar android:id="@+id/ProgressBar1" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:contentDescription="mainFragmentRoot"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
活动源代码除了之外是空的setContentView,而片段源代码只包含
@Override
public View onCreateView(...) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
Run Code Online (Sandbox Code Playgroud)
现在,
我希望PrograssBar直接在活动根的层次结构中看到,但是有一个额外的FrameLayout,我不知道它来自哪里.这是一个屏幕截图,在黄色中绘制了额外的框架:

所以,我的问题是 - 它来自哪里?我可以摆脱它吗? 在我的实际应用程序中,这些额外的FrameLayouts正在创建非常深层次的层次结构,这可能对性能有害.
谢谢!
eve*_*otc 12
看起来你正在使用支持v4库而你忘了把你的片段xml标签放入和id :),所以:
它从哪里来的?
它来自FragmentManager的888行,你可以看到:
f.mView = NoSaveStateFrameLayout.wrap(f.mView);
Run Code Online (Sandbox Code Playgroud)
这样做的原因是向后兼容性,最好在注释标题中解释NoSaveStateFrameLayout:
/**
* Pre-Honeycomb versions of the platform don't have {@link View#setSaveFromParentEnabled(boolean)},
* so instead we insert this between the view and its parent.
*/
Run Code Online (Sandbox Code Playgroud)
我可以摆脱它吗?
好吧,我可以想到三个选择:
FragmentManager基于支持v4库版本来实现您自己的say 实现,其中您省略了这个容器,但我认为编写/维护该代码的努力是不值得的,而且我不认为这些FrameLayouts 的开销是巨大的,如果您遇到性能问题View,除此之外您可能还需要执行其他优化(比如编写自定义视图extends View- ) - 或者重新考虑布局/片段以减少特定层次结构中的视图数量.FragmentManager实现没有这个嵌套ViewGroup(参见11+行861FragmentManager),对于那些你得到这样的东西:
我不会担心那些,因为我提到有其他优化你可以投入那个时间;)
| 归档时间: |
|
| 查看次数: |
2388 次 |
| 最近记录: |