首页 > 解决方案 > 布局文件中的片段会在 Android Studio 中自动加载吗?

问题描述

以下代码来自项目

布局文件 activity_main.xml 包括两个片段fragmentfragmentBind

项目加载布局文件使用 MainActivity.kt。

我发现fragmentfragmentBind两个片段都是自动加载的,我在 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()
    }
}

标签: androidandroid-fragments

解决方案


除了在运行时通过事务手动加载片段外,还可以使用片段标签将片段添加到布局本身 :

<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而不是fragmentXML。


推荐阅读