android - 布局文件中的片段会在 Android Studio 中自动加载吗?
问题描述
以下代码来自项目
布局文件 activity_main.xml 包括两个片段fragment和fragmentBind。
项目加载布局文件使用 MainActivity.kt。
我发现fragment和fragmentBind两个片段都是自动加载的,我在 MainActivity.kt 中找不到任何代码来加载这两个片段。
布局文件中的片段会在 Android Studio 中自动加载吗?
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textViewActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="@string/hello_activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.117" />
<fragment
android:id="@+id/fragment"
android:name="com.android.example.viewbindingsample.InflateFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
app:layout_constraintBottom_toTopOf="@id/fragmentBind"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewActivity" />
<fragment
android:id="@+id/fragmentBind"
android:name="com.android.example.viewbindingsample.BindFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onStart() {
super.onStart()
binding.textViewActivity.text = getString(string.hello_from_vb_activity)
}
}
绑定片段.kt
class BindFragment : Fragment(R.layout.fragment_blank) {
// Scoped to the lifecycle of the fragment's view (between onCreateView and onDestroyView)
private var fragmentBlankBinding: FragmentBlankBinding? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentBlankBinding.bind(view)
fragmentBlankBinding = binding
binding.textViewFragment.text = getString(string.hello_from_vb_bindfragment)
}
override fun onDestroyView() {
// Consider not storing the binding instance in a field, if not needed.
fragmentBlankBinding = null
super.onDestroyView()
}
}
解决方案
除了在运行时通过事务手动加载片段外,还可以使用片段标签将片段添加到布局本身 :
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
关键属性是android:name
android:name="com.example.android.fragments.ArticleFragment"
这将膨胀并加载布局中的片段。
如您所知,您还可以在运行时使用片段和事务管理器替换加载的片段,如下所示:
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.article_fragment, TabArticleFragment())
transaction.commit()
为获得最佳实践,请使用FragmentContainerView而不是fragment
XML。
推荐阅读
- python - 了解 scipy.spatial.Voronoi 以 3D 形式返回的山脊顶点
- extjs - 使用 Ext JS 7.0.0 拖放精灵
- java - 从 firebaserecyleroptions 获取数据时发生错误
- flutter - 颤动中的 ModalBottomSheet 内的 ListView
- javascript - 如何在悬停时将画布渐变作为按钮背景
- android - svchost.exe 监听 5037 端口,阻止 adb.exe 工作
- sql - 行构造函数比较的 SQL Server 等效项是什么
- mysql - MySQL InnoDB 简单的 SELECT 查询需要 0.2 秒
- c# - 如何在 ASP.NET Core MVC 中使用我的视图模型更新我的 3 个表(我已经有了 Create)
- java - 当我将 ImapIdleChannelAdapter 配置为从收件箱中读取电子邮件时,Spring Boot 中的调度程序停止工作