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

相关文章

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

java变量内存中存储的使用方式

《java变量内存中存储的使用方式》:本文主要介绍java变量内存中存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、变量的定义3、 变量的类型4、 变量的作用域5、 内存中的存储方式总结1、介绍在 Java 中,变量是用于存储程序中数据

关于Mybatis和JDBC的使用及区别

《关于Mybatis和JDBC的使用及区别》:本文主要介绍关于Mybatis和JDBC的使用及区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、JDBC1.1、流程1.2、优缺点2、MyBATis2.1、执行流程2.2、使用2.3、实现方式1、XML配置文件