AndroID工程显示Flutter布局的界面步骤(不包含从Flutter跳转到对应的Activity的 *** 作)。
第一步:创建AndroID项目;
第二步:在创建好的AndroID项目的名称中点击右键new-->Module-->(选中)Flutter module-->next-->finish,生成好之后先不用管Flutter;
第三步:1、在项目中的MainActivity中的布局文件中main_activity进行如下布局:
<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/frame_l" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MainActivity" />
2、创建一个类TestApp继承Application,代码如下:
public class TestApp extends Application { public static final String ENGINE_ID = "CACHED_FlutteR"; private static TestApp initApp; private FlutterEngine FlutterEngine; public FlutterEngine getFlutterEngine() { return FlutterEngine; } public static TestApp getApp() { return initApp; } @OverrIDe public voID onCreate() { super.onCreate(); initApp = this; // Flutter合并开发初始化 FlutterEngine = new FlutterEngine(initApp); FlutterEngine.getDartExecutor().executeDartEntrypoint( DartExecutor.DartEntrypoint.createDefault() ); FlutterEngineCache.getInstance().put(ENGINE_ID, FlutterEngine); }}
3、在配置清单AndroIDManifest.xml文件中引入TestApp;顺便设置下项目的样式,在res文件夹下的themes.xml中添加如下代码去掉标题栏设置,代码如下:
<style name="NoActionbar" parent="theme.MaterialComponents.DayNight.NoActionbar"> <item name="androID:statusbarcolor" tools:targetAPI="l">@color/blue</item> <!-- 设置状态栏的颜色 --></style>
4、在MainActivity上配置,显示Flutter上面的布局,代码如下:
public class MainActivity extends AppCompatActivity { static final String TAG_ACTIVITY_TO_FlutteR = "TAG_ACTIVITY_TO_FlutteR"; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // 链接Flutter,显示Flutter界面 FragmentManager fragmentManager = getSupportFragmentManager(); FlutterFragment mFlutterFragment = (FlutterFragment) fragmentManager.findFragmentByTag(TAG_ACTIVITY_TO_FlutteR); if (mFlutterFragment == null) { mFlutterFragment = FlutterFragment.withCachedEngine(TestApp.getApp().ENGINE_ID).transparencyMode(TransparencyMode.opaque).build(); fragmentManager.beginTransaction().add(R.ID.frame_l, mFlutterFragment, TAG_ACTIVITY_TO_FlutteR).commit(); } else { fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag(TAG_ACTIVITY_TO_FlutteR)).commit(); } }}
第五步:直接运行APP(切记显示的是app,而不是main.dart)
总结以上是内存溢出为你收集整理的Flutter和Android混合开发(1)全部内容,希望文章能够帮你解决Flutter和Android混合开发(1)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)