Android 为什么要为片段创建额外的FrameLayout?
在使用以减少层次结构时,我注意到每次添加片段(以“静态”或“动态”方式)时,片段总是以新框架布局包装 这里有一个例子: 这是我的活动布局:Android 为什么要为片段创建额外的FrameLayout?,android,performance,view,android-fragments,Android,Performance,View,Android Fragments,在使用以减少层次结构时,我注意到每次添加片段(以“静态”或“动态”方式)时,片段总是以新框架布局包装 这里有一个例子: 这是我的活动布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heig
<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>
<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" />
现在,
我希望直接在活动根的层次结构中看到PrograssBar
,但实际上有一个额外的框架布局,我不知道它来自何处。
这是一个屏幕截图,将额外的框架涂成黄色:
那么,我的问题是——它是从哪里来的?我能摆脱它吗?
在我的实际应用程序中,这些额外的框架布局创建了非常深的层次结构,这可能对性能有害
谢谢 似乎您正在使用support v4库,但忘记将和id添加到片段xml标记:),因此: 它是从哪里来的? 从这里你可以看到:
f.mView = NoSaveStateFrameLayout.wrap(f.mView);
原因是向后兼容,最好在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.
*/
我能把它处理掉吗?
嗯,我可以想出三个选择:
FragmentManager
实现,比如说基于support v4 library版本,在该版本中您可以省略此容器,但我认为编写/维护该代码的努力是不值得的,而且我不认为这些FrameLayout
的开销是巨大的,如果存在性能问题,除此之外,您可能还需要执行其他视图
优化(比如编写一个自定义视图-它扩展了视图),或者说重新考虑布局/片段,以减少特定点层次结构中的视图数量clipChildren
设置为false,因为它正在剪切其子视图。
/**
* Pre-Honeycomb versions of the platform don't have {@link View#setSaveFromParentEnabled(boolean)},
* so instead we insert this between the view and its parent.
*/