zoukankan      html  css  js  c++  java
  • Android基础——Fragment的创建和添加

    新建一个Fragment的过程

    package com.example.myactivityiiii;
    
    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;
    
    /*
    * 新建一个Fragment的过程
    *   1.自定义一个类,基础Fragment
    *   2.配置该类的layout文件
    *   3.重写onCreateView方法,加载该类的布局文件,返回一个View
    * */
    public class ListFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(
                    R.layout.fragment_list,container,false
            );
            return view;
        }
    }
    package com.example.myactivityiiii;
    
    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;
    
    import java.util.zip.Inflater;
    
    public class DetailFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(
                    R.layout.fragment_detail,container,false
            );
            return view;
        }
    }

    在Activity中添加Fragment

    1.直接通过layout添加:在Main的布局文件中添加以下代码

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:name="com.example.myactivityiiii.ListFragment"
        android:id="@+id/list"
        android:layout_weight="1"/>
    
    <fragment
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:name="com.example.myactivityiiii.DetailFragment"
        android:id="@+id/detail"
        android:layout_weight="2"/>

    2.在Activity运行时添加Fragment,主要修改的是Main中的代码

    在Main中的layout中添加一个布局容器,把新建的Fragment丢到这个布局容器里就好

    Main的layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:orientation="horizontal"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Main"
            >
        </TextView>
    
        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
        </FrameLayout>
    
    </LinearLayout>

    Main的java文件

    package com.example.myactivityiiii;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentTransaction;
    
    import android.os.Bundle;
    
    /*
    * Activity运行时添加Fragment
    * */
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            DetailFragment detailFragment = new DetailFragment();
            //获取Fragment的transaction实例
            FragmentTransaction ft= getSupportFragmentManager().beginTransaction();
            //指定一个容器来添加detailFragment
            ft.add(R.id.frame,detailFragment);
            //提交事物
            ft.commit();
    
        }
    }
  • 相关阅读:
    Android 横屏启动activity,点击屏幕的单击、双击
    实现Android简单动画旋转案例
    当时遇到的主要难点在于TextView的内容不会刷新改变值,今天终于通过Timer和Handler实现了,分享给大家
    如何在Android当中显示网络图片
    Android的MediaRecorder架构介绍
    理解Android系统的进程间通信原理RPC机制
    Android开发WeatherForecast程序
    Android 如何导入已有的外部数据库
    百度地图API 源码
    Android TelephonyManager类
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12236182.html
Copyright © 2011-2022 走看看