Flutter与native混合开发的时候,默认flutter的渲染模式(RenderMode.surface)会把native布局中relativelayout中的上层view覆盖。
先说了下结论,再举个栗子; 比如原生布局是这样的,直接上代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="100dp"
android:layout_alignParentBottom="true"
android:background="@color/color_666666"/>
</RelativeLayout>
当显示原生的fragment的时候 上层的那个小view能正常显示,但是当显示 flutterFragment的时候你会发现 上层的那个小view不见了,被盖住了。我曹,啥情况。
忽然想起来看文档的时候无意中看到过 flutter 可以设置渲染模式,果断查了下文档;
文档地址
有三种渲染模式,默认的是 surface;
surface
public static final RenderMode surface
RenderMode, which paints a Flutter UI to a SurfaceView. This mode has the best performance, but a Flutter UI in this mode cannot be positioned between 2 other Android Views in the z-index, nor can it be animated/transformed. Unless the special capabilities of a SurfaceTexture are required, developers should strongly prefer this render mode.
texture
public static final RenderMode texture
RenderMode, which paints a Flutter UI to a SurfaceTexture. This mode is not as performant as surface, but a Flutter UI in this mode can be animated and transformed, as well as positioned in the z-index between 2+ other Android Views. Unless the special capabilities of a SurfaceTexture are required, developers should strongly prefer the surface render mode.
image
public static final RenderMode image
RenderMode, which paints Paints a Flutter UI provided by an ImageReader onto a Canvas. This mode is not as performant as surface, but a FlutterView in this mode can handle full interactivity with a PlatformView. Unless PlatformViews are required developers should strongly prefer the surface render mode.
简单翻译一下就是,算了不翻译了,英语不好,用翻译工具翻一下吧;
我觉得大概意思是 surface性能最好,但是Z轴定位应该是有问题,算是找到原因了吧,把渲染模式切换成texture就没问题了。
FlutterFragment
.createDefault()
.renderMode(RenderMode.texture)
.build()