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使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

如何使用Nginx配置将80端口重定向到443端口

《如何使用Nginx配置将80端口重定向到443端口》这篇文章主要为大家详细介绍了如何将Nginx配置为将HTTP(80端口)请求重定向到HTTPS(443端口),文中的示例代码讲解详细,有需要的小伙... 目录1. 创建或编辑Nginx配置文件2. 配置HTTP重定向到HTTPS3. 配置HTTPS服务器

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加