Fragment对象的使用(一)

2024-09-02 17:38
文章标签 使用 对象 fragment

本文主要是介绍Fragment对象的使用(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

(1)新建名称为fragment的Android项目。布局文件activity_main.xml的代码如下:

<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="match_parent"android:id="@+id/layout1"android:orientation="vertical"android:gravity="center"     ><Buttonandroid:id="@+id/btn01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="I am bttton"/>
</LinearLayout > 

新建布局文件layout1.xml如下:

<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="match_parent"   ><Buttonandroid:id="@+id/btn01"android:layout_marginTop="50dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="I am "android:layout_centerHorizontal="true" android:layout_alignParentTop="true"/>
</LinearLayout>
新建布局文件layout2.xml如下:

<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="match_parent"tools:context="${relativePackage}.${activityClass}" ><Buttonandroid:id="@+id/btn02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="I"android:layout_centerHorizontal="true" android:layout_alignParentTop="true"/></LinearLayout>
(2)新建两个自定义的Fragment对象:

fragment1.java:

package com.example.fragment;import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class fragment1 extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.layout1, container, false);return view;}
}

fragment2.java:

package com.example.fragment;import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class fragment2 extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle saveInstanceState){view=inflater.inflate(R.layout.layout2,container,false);return view;}
}

这两个Fragment对象的代码仅仅是关联的布局的文件不同

(3)文件MainActivity.Java的核心代码如下:

package com.example.fragment;import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Button;@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity {private Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Inital();fragment1 fragment_1=new fragment1();fragment2 fragment_2=new fragment2();FragmentManager fm=this.getFragmentManager();FragmentTransaction	ft= fm.beginTransaction();ft.add(R.id.layout1,fragment_1);ft.add(R.id.layout1,fragment_2);ft.commit();}
}

这样就实现了基本的fragment对象。

注:方法inflate()有3个参数,第一个参数表示Fragment对象关联的是哪个XML布局文件,第二个参数是把这个XML文件转成的View对象放入container容器中,第三个参数表示是否把这个View对象追加到container参数的后面。我们的第三个参数写得是false。如果这个参数是true,则代码应该是:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {inflater.inflate(R.layout.layout1, container, true);return super.onCreateView(inflater, container, savedInstanceState);
运行项目,也会出现正确的结果。



这篇关于Fragment对象的使用(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130621

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互