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实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

GORM中Model和Table的区别及使用

《GORM中Model和Table的区别及使用》Model和Table是两种与数据库表交互的核心方法,但它们的用途和行为存在著差异,本文主要介绍了GORM中Model和Table的区别及使用,具有一... 目录1. Model 的作用与特点1.1 核心用途1.2 行为特点1.3 示例China编程代码2. Tab

SpringBoot使用OkHttp完成高效网络请求详解

《SpringBoot使用OkHttp完成高效网络请求详解》OkHttp是一个高效的HTTP客户端,支持同步和异步请求,且具备自动处理cookie、缓存和连接池等高级功能,下面我们来看看SpringB... 目录一、OkHttp 简介二、在 Spring Boot 中集成 OkHttp三、封装 OkHttp

使用Python实现获取网页指定内容

《使用Python实现获取网页指定内容》在当今互联网时代,网页数据抓取是一项非常重要的技能,本文将带你从零开始学习如何使用Python获取网页中的指定内容,希望对大家有所帮助... 目录引言1. 网页抓取的基本概念2. python中的网页抓取库3. 安装必要的库4. 发送HTTP请求并获取网页内容5. 解

使用Python实现网络设备配置备份与恢复

《使用Python实现网络设备配置备份与恢复》网络设备配置备份与恢复在网络安全管理中起着至关重要的作用,本文为大家介绍了如何通过Python实现网络设备配置备份与恢复,需要的可以参考下... 目录一、网络设备配置备份与恢复的概念与重要性二、网络设备配置备份与恢复的分类三、python网络设备配置备份与恢复实

C#中的 StreamReader/StreamWriter 使用示例详解

《C#中的StreamReader/StreamWriter使用示例详解》在C#开发中,StreamReader和StreamWriter是处理文本文件的核心类,属于System.IO命名空间,本... 目录前言一、什么是 StreamReader 和 StreamWriter?1. 定义2. 特点3. 用

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

Python使用DrissionPage中ChromiumPage进行自动化网页操作

《Python使用DrissionPage中ChromiumPage进行自动化网页操作》DrissionPage作为一款轻量级且功能强大的浏览器自动化库,为开发者提供了丰富的功能支持,本文将使用Dri... 目录前言一、ChromiumPage基础操作1.初始化Drission 和 ChromiumPage

Django序列化中SerializerMethodField的使用详解

《Django序列化中SerializerMethodField的使用详解》:本文主要介绍Django序列化中SerializerMethodField的使用,具有很好的参考价值,希望对大家有所帮... 目录SerializerMethodField的基本概念使用SerializerMethodField的

Ollama Python 使用小结

《OllamaPython使用小结》Ollama提供了PythonSDK,使得开发者能够在Python环境中轻松集成和使用本地运行的模型进行自然语言处理任务,具有一定的参考价值,感兴趣的可以了解一... 目录安装 python SDK启动本地服务使用 Ollama 的 Python SDK 进行推理自定义客