简体   繁体   English

将 setContentView(R.layout.activity_main) 与 ViewBinding 合并

[英]Consolidating setContentView(R.layout.activity_main) with ViewBinding

I'm fairly new to Android development and i'm trying to combine the functionality from 2 separate tutorials into one app.我对 Android 开发相当陌生,我正在尝试将 2 个单独教程的功能组合到一个应用程序中。

One of them, which i've already implemented, simply uses setContentView(R.layout.activity_main) in order to provide the main activity for it's fragments.其中之一,我已经实现,简单地使用setContentView(R.layout.activity_main)来为其片段提供主要活动。 The second uses the below code第二个使用下面的代码

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)

What is the best way for me to go about combining these 2 different approaches so i don't end up creating errors in the code already implemented.对我来说,关于结合这两种不同方法的 go 的最佳方法是什么,所以我最终不会在已经实现的代码中产生错误。 And will it be necessary to convert all existing fragments to 'data binding layout' as a result?结果是否有必要将所有现有片段转换为“数据绑定布局”?

Thanks谢谢

You can use both of them together, no necessary to convert all the existing fragment to 'data binding layout', I also have something of this in one of my projects.您可以同时使用它们,无需将所有现有片段转换为“数据绑定布局”,我的一个项目中也有类似的东西。

The best way to proceed is to start the new Activity/Fragment with view binding and with overtime you can one class at a time update existing Activity/Fragment to avoid too many of changes in one go.最好的方法是使用视图绑定启动新的 Activity/Fragment,超时后您可以一次更新一个 class 来更新现有的 Activity/Fragment,以避免在一个 go 中进行太多更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 setContentView(R.layout.activity_main) - error setContentView(R.layout.activity_main) setContentView(R.layout.activity_main) 上的异常; - Exception on setContentView(R.layout.activity_main); setContentView(R.layout.activity_main); 不管用 - The setContentView(R.layout.activity_main); is not working 为什么要使用setContentView(R.layout.activity_main); 布局错误 - Why setContentView(R.layout.activity_main); giving error in layout setContentView(R.layout.activity_main)导致应用崩溃 - setContentView(R.layout.activity_main) causes app to crash setContentView(R.layout.activity_main)出现运行时错误 - RunTime Error at setContentView(R.layout.activity_main) 在某些设备上调用 setContentView(R.layout.activity_main) 的问题 - Issue with calling setContentView(R.layout.activity_main) on some devices setContentView(R.layout.activity_main)vs getMenuInflater()。inflate(R.menu.activity_main,menu) - setContentView(R.layout.activity_main) vs getMenuInflater().inflate(R.menu.activity_main, menu) Android App setContentView(R.layout.activity_main) vs View binding - layout_gravity not respected - Android App setContentView(R.layout.activity_main) vs View binding - layout_gravity not respected 如何修复程序包R不存在setContentView(R.layout.activity_main)及其后果? - How to fix package R does not exist setContentView(R.layout.activity_main) and it consequences?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM