SimpleFragment
主界面包含Button、Fragmentlayout控件,按钮按下,fragment出现。
1.主界面。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:background="#66666686">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity_main" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>
<FrameLayout
android:id="@+id/fragmentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/black" />
</LinearLayout>
2.新建一个fragment_simple布局文件和SimpleFragment类:
fragment_simple.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#34883444"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is a fragment"/>
</LinearLayout>
SimpleFragment.kt:
package com.example.simplefragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
//继承自Fragment()
class SimpleFragment : Fragment() {
//重写onCreateView方法,包含三个参数
//LayoutInflater inflater:这个参数用于将布局XML资源转换为View对象。通常在方法内部使用它来膨胀(inflate)由container参数提供的布局资源。
//ViewGroup container:这个参数是Fragment的UI应该附加到的父ViewGroup。它提供了Fragment所处的上下文(context)。
//Bundle savedInstanceState:这个参数是一个Bundle对象,包含Fragment的保存状态。如果Fragment是从之前的保存状态重新创建的,则此Bundle包含该数据。否则,它为null。
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val inflater = inflater.inflate(R.layout.fragment_simple, container, false)
return inflater
}
}
3.MainActivity中编写监听事件。
MainActivity.kt:
package com.example.simplefragment
import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener{
showFragment(SimpleFragment())
}
}
private fun showFragment(fragment: SimpleFragment) {
// 获取 FragmentManager 实例
val fragmentManager = supportFragmentManager
// 开启一个事务
val transaction = fragmentManager.beginTransaction()
// 用传入的 fragment 替换指定的布局资源 ID(R.id.fragmentlayout)所在的位置
transaction
.replace(R.id.fragmentlayout, fragment)
.commit()
}
}