4.16

所花时间(包括上课):2.5

打码量(行):200

博客量(篇):1

了解到知识点:学习Fragment动态注册

 

 import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;

 

public class MyFragment extends Fragment {

 

    @Nullable

    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        // 将 fragment_my 布局文件解析为视图

        return inflater.inflate(R.layout.fragment_my, container, false);

    }

}

<!-- fragment_my.xml -->

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- 可以在这里添加实际的视图内容 -->

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="这是一个动态添加的 Fragment"

        android:layout_gravity="center" />

</FrameLayout>

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentTransaction;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 获取按钮视图

        Button addButton = findViewById(R.id.add_button);

 

        // 设置按钮点击监听器

        addButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 动态添加 Fragment

                addFragment();

            }

        });

    }

 

    private void addFragment() {

        // 创建 Fragment 实例

        Fragment myFragment = new MyFragment();

 

        // 获取 FragmentManager

        FragmentManager fragmentManager = getSupportFragmentManager();

 

        // 开始一个 Fragment 事务

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

 

        // 添加 Fragment 并指定容器视图 ID

        fragmentTransaction.add(R.id.fragment_container, myFragment);

 

        // 提交事务

        fragmentTransaction.commit();

    }

}

<!-- activity_main.xml -->

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- 按钮,用于添加 Fragment -->

    <Button

        android:id="@+id/add_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="添加 Fragment"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="20dp" />

 

    <!-- Fragment 容器 -->

    <FrameLayout

        android:id="@+id/fragment_container"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_below="@id/add_button"

        android:layout_marginTop="20dp" />

</RelativeLayout>

 

posted @ 2024-04-16 15:33  赵千万  阅读(2)  评论(0编辑  收藏  举报