感觉之前看的还是不清楚,重新再研究了一次

Fragment常用的三个类:

android.app.Fragment 主要用于定义Fragment

android.app.FragmentManager 主要用于在Activity中操作Fragment

android.app.FragmentTransaction 保证一些列Fragment操作的原子性,熟悉事务这个词,一定能明白~

1. 静态调用(就充当普通的控件)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="viewpagerindicator.com.fragmentstatic"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </application> </manifest>

activity_main.xml

<?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="vertical"
>
<!--静态调用fragment的方法:
就是相当于把fragment当做一个Button或者EditText之类的空间
其中name就相当于普通空间的ID-->
<fragment
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/fragment1"
android:name="viewpagerindicator.com.fragmentstatic.FragmentTittle"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="区隔按键"/> <fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_context_id"
android:name="viewpagerindicator.com.fragmentstatic.FragmentContext"/> </LinearLayout>

fragment_tittle.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"> <Button
android:text="按键"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:textSize="15sp"
/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试FRAGMENT"
android:gravity="center"
/>
</RelativeLayout>

fragment_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:text="显示区域"
android:textSize="40dp"
android:gravity="center"/>
</LinearLayout>

MainActivity.java
package viewpagerindicator.com.fragmentstatic;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; public class MainActivity extends AppCompatActivity {
final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
}
FragmentTittle.java
package viewpagerindicator.com.fragmentstatic;

import android.app.Fragment;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast; public class FragmentTittle extends Fragment { Button mLeftMenu;
final String TAG = "FragmentTittle";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_tittle, container, false);
mLeftMenu = (Button) view.findViewById(R.id.bt1);
mLeftMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "按键点击事件", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
FragmentContext.java
package viewpagerindicator.com.fragmentstatic;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FragmentContext extends Fragment { final String TAG = "FragmentContext";
//Button mLeftMenu; public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_content, container, false);
//mLeftMenu = (Button) view.findViewById(R.id.bt1);
return view;
}
}

2. 动态调用

组件--Fragment(碎片)第二篇详解的更多相关文章

  1. Scala进阶之路-Scala函数篇详解

    Scala进阶之路-Scala函数篇详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.传值调用和传名调用 /* @author :yinzhengjie Blog:http: ...

  2. 转载 JS组件Bootstrap Select2使用方法详解

    JS组件Bootstrap Select2使用方法详解 作者:懒得安分 字体:[增加 减小] 类型:转载 时间:2016-01-26我要评论 这篇文章主要为大家介绍了JS组件Bootstrap Sel ...

  3. PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明

    PHP函数篇详解十进制.二进制.八进制和十六进制转换函数说明 作者: 字体:[增加 减小] 类型:转载   中文字符编码研究系列第一期,PHP函数篇详解十进制.二进制.八进制和十六进制互相转换函数说明 ...

  4. 走向DBA[MSSQL篇] 详解游标

    原文:走向DBA[MSSQL篇] 详解游标 前篇回顾:上一篇虫子介绍了一些不常用的数据过滤方式,本篇详细介绍下游标. 概念 简单点说游标的作用就是存储一个结果集,并根据语法将这个结果集的数据逐条处理. ...

  5. Android UI组件----AppWidget控件入门详解

    Widget引入 我们可以把Widget理解成放置在桌面上的小组件(挂件),有了Widget,我们可以很方便地直接在桌面上进行各种操作,例如播放音乐. 当我们长按桌面时,可以看到Widget选项,如下 ...

  6. Android组件---四大布局的属性详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...

  7. QuantLib 金融计算——基本组件之天数计算规则详解

    目录 天数计算规则详解 定义 30 / 360 法 30/360 US 30/360 Bond Basis 30E/360 30E/360 ISDA Actual 法 Actual/Actual IC ...

  8. Spring--开篇 (spring优缺点、模块组件、各个jar包详解)

    Spring--开篇 分类: SSH&EJB2012-11-23 15:25 4369人阅读 评论(13) 收藏 举报 javaJavaJAVAspringSpringwebWebWEB框架 ...

  9. Android UI组件----ListView列表控件详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

随机推荐

  1. SQL Server—— 如何创建定时作业

    在做SQL server 管理时,往往需要每日执行定时任务,但是如果每天都去人工执行,非常不方便,而且一般定时操作,都应该是在数据库压力不大时,一般是在夜间.所以我们需要创建定时作业来代替人工的执行定 ...

  2. js ajax请求传token

    js  ajax请求传token 方法一: headers: { Authorization: "BasicAuth " + token } 方法二: beforeSend: fu ...

  3. String,Json,Map之间的转化

    前提条件: 1)String的格式是map或json类型的 ; 2)在JAVA中使用JSON需要引入 org.json 包 String >>Json JSONObject jsonObj ...

  4. leetcode 72 编辑距离 JAVA

    题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例  ...

  5. python文件备份与简单操作

    #!/usr/bin/python # -*- coding: utf-8 -*- # data:2018/8/30 # user:fei import sys import random num = ...

  6. 【OCP-12c】CUUG 071题库考试原题及答案解析(16)

    16.(7-5) choose the best answerThe PRODUCTS table has the following structure:Evaluate the following ...

  7. IDEA中配置SpringMVC框架 第一个演示【转】

    环境: intellij IDEA 2017 CI JDK 1.8 tomcat 8.5.23 具体步骤 1.新建项目 勾选Spring MVC .Web Application(勾选了Spring ...

  8. CentOS7安装weblogic集群思路梳理

    以前经常用weblogic集群,但是却没有仔细想过要实现它.这不,前两天成功安装了weblogic集群,现在将其思路整理下.防止日后自己忘掉了. 一.安装weblogic10.3.6 1. 在官网下载 ...

  9. input disabled的情况下在IOS设备中怎么修改颜色

    -webkit-text-fill-color:black; -webkit-opacity:1; opacity: 1; 一句代码就欧了

  10. 把序列中的N个元素赋值给多个变量

    说明: python中序列和散列 序列:可通过偏移量来进行切片的对象.列表.元组.字符串都属于序列. 散列:无法通过偏移量来进行切片的对象.比如 集合.字典 一. 变量的数量跟序列元素的个数一样 1. ...