一、第一种方法:

(1)Fragment的第一种使用方法是使用fragment加载单独的布局文件:(也就是xml的方式实现)

结构如下:

activity_main.xml主要是在一个线性布局中添加两个线性布局

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. tools:context=".MainActivity" >
  7. <LinearLayout
  8. android:id="@+id/linerlayout1"
  9. android:layout_width="0dp"
  10. android:layout_height="match_parent"
  11. android:layout_weight="1"
  12. android:background="#CCCCCC"
  13. android:orientation="vertical" >
  14. <Button
  15. android:id="@+id/button1"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="显示窗口" />
  19. </LinearLayout>
  20. <LinearLayout
  21. android:id="@+id/linerlayout2"
  22. android:layout_width="0dp"
  23. android:layout_height="match_parent"
  24. android:layout_weight="3"
  25. android:background="#CCFFDD"
  26. android:orientation="vertical" >
  27. </LinearLayout>
  28. </LinearLayout>

right.xml是等会使用fragment的时候,加载的一个布局文件:(由于主要是在界面中加载、所以不作特殊要求)

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <RatingBar
  7. android:id="@+id/ratingBar1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" />
  10. <Button
  11. android:id="@+id/button11"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="点我试试" />
  15. </LinearLayout>

MyFragment.java就是加载fragment的类,要继承Fragment类:(要重载父类的下边三个方法)

[java] view plaincopy
  1. package com.lc.tablet_fragment_addview;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. import android.widget.Toast;
  10. public class MyFragment extends Fragment {
  11. public MyFragment() {
  12. // TODO Auto-generated constructor stub
  13. }
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. // TODO Auto-generated method stub
  17. super.onCreate(savedInstanceState);
  18. }
  19. @Override
  20. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  21. Bundle savedInstanceState) {
  22. // 这里的R.layout.right是界面的id
  23. View view = inflater.inflate(R.layout.right, null);
  24. Button button = (Button) view.findViewById(R.id.button11);
  25. button.setOnClickListener(new OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. Toast.makeText(getActivity(), "hello world!", Toast.LENGTH_LONG)
  29. .show();
  30. }
  31. });
  32. return view;
  33. }
  34. @Override
  35. public void onPause() {
  36. // TODO Auto-generated method stub
  37. super.onPause();
  38. }
  39. }

MainActivity.java:

[java] view plaincopy
  1. package com.lc.tablet_fragment_addview;
  2. import android.app.Activity;
  3. import android.app.FragmentManager;
  4. import android.app.FragmentTransaction;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. public class MainActivity extends Activity {
  11. private Button button;
  12. private FragmentManager fragmentManager; // 管理
  13. private FragmentTransaction fragmentTransaction; // 事务
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. button = (Button) this.findViewById(R.id.button1);
  19. fragmentManager = getFragmentManager();
  20. button.setOnClickListener(new OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. fragmentTransaction = fragmentManager.beginTransaction();
  24. MyFragment myFragment = new MyFragment();
  25. // 第一个参数是要放到哪个地方的id,第二个为要放入的fragment
  26. fragmentTransaction.add(R.id.linerlayout2, myFragment);
  27. fragmentTransaction.commit();
  28. }
  29. });
  30. }
  31. @Override
  32. public boolean onCreateOptionsMenu(Menu menu) {
  33. // Inflate the menu; this adds items to the action bar if it is present.
  34. getMenuInflater().inflate(R.menu.main, menu);
  35. return true;
  36. }
  37. }

演示效果:当点击灰色界面的按钮时显示右侧的布局:

二、第二种方法

项目结构和上图中的差不多:只是在布局文件中,直接使用fragment控件:
[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <fragment
  11. android:id="@+id/fragment1"
  12. android:name="com.example.tablet_fragment_fragementmanager.MyFragment"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentTop="true"
  16. android:layout_centerHorizontal="true"
  17. android:layout_marginTop="37dp" />
  18. </RelativeLayout>
在myfragment.java文件中,只需找到fragment所容纳的布局文件即可,不进行业务上的操作:
[java] view plaincopy
  1. package com.example.tablet_fragment_fragementmanager;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class MyFragment extends Fragment {
  8. public MyFragment() {
  9. }
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. }
  14. @Override
  15. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  16. Bundle savedInstanceState) {
  17. /*
  18. * 这里只需找到布局文件即可
  19. */
  20. View view = inflater.inflate(R.layout.text, null);
  21. return view;
  22. }
  23. @Override
  24. public void onResume() {
  25. super.onResume();
  26. }
  27. }
MainActivity.java文件:进行fragment的业务处理
[java] view plaincopy
  1. package com.example.tablet_fragment_fragementmanager;
  2. import android.app.Activity;
  3. import android.app.FragmentManager;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.Toast;
  10. /*
  11. * 再布局文件中拖入一个fragment、则使用下边的方法来找到特定的fragment
  12. * 不需要使用beginTransaction方法
  13. */
  14. public class MainActivity extends Activity {
  15. private MyFragment fragment;
  16. private FragmentManager fragmentManager;
  17. private Button button;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. fragmentManager = getFragmentManager();
  23. // 使用fragmentManager找到fragment、使用ID作为唯一的标识符
  24. fragment = (MyFragment) fragmentManager
  25. .findFragmentById(R.id.fragment1);
  26. // 或者使用下边的方法找到fragment
  27. // fragment =(MyFragment)fragmentManager.findFragmentByTag("fragment1");
  28. // 找到fragment布局中的按钮button1
  29. button = (Button) fragment.getView().findViewById(R.id.button1);
  30. button.setOnClickListener(new OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. Toast.makeText(MainActivity.this, "hello world!",
  34. Toast.LENGTH_SHORT).show();
  35. }
  36. });
  37. }
  38. @Override
  39. public boolean onCreateOptionsMenu(Menu menu) {
  40. getMenuInflater().inflate(R.menu.main, menu);
  41. return true;
  42. }
  43. }

Android学习笔记之Fragment的两种使用方法相关推荐

  1. IOS学习之UISwitch控件两种使用方法和监听

    IOS学习之UISwitch控件两种使用方法和监听 分类: IOS开发入门2012-06-15 11:48 1363人阅读 评论(0) 收藏 举报 一.第一种创建UISwitch控件的方法,在代码中动 ...

  2. 5G NR - RACH学习笔记3 - RACH的两种接入类型

    CBRA(Contention Based - 基于竞争的随机接入) 基于竞争的RACH过程如下: 1) UE --> NW : MSG1(RACH Preamble, RA-RNTI-) 2) ...

  3. 开关电源环路学习笔记(4)-两种误差放大器的传递函数

    前面几节我们已经说明白了,在满足一定条件的时候,Buck开关电源可以看成是线性系统,并且可以是划分为4级的反馈控制系统. 1.反馈级:H(s) 2.放大和补偿级:Gc(s) 3.PWM调制级:Gpwm ...

  4. Android学习笔记之——手机中几种自带传感器的应用

    本博文学习一下Android中几种传感器的应用 目录 Android传感器 使用传感器步骤 Demo Test 查看手机支持哪些传感器 界面布局

  5. openvc学习笔记(4)——两种方法在没有环境下运行程序

    参考博客:http://blog.csdn.net/xiaowei_cqu/article/details/8066329 OpenCV的安装总结 我们下载的OpenCV安装包一般包括源码,示例,文档 ...

  6. android学习笔记之Fragment(一)

    2019独角兽企业重金招聘Python工程师标准>>> Fragment(Android3.0 API Level 11引入,之前的怎么办? 没关系可以使用Android suppo ...

  7. mybatis学习笔记(2)两种配置实现增删改查

    第二天学习mybatis,发现框架的存在确实是省了我们很多时间,我们完全可以把一些操作简化,或者实现简单的封装提供我们使用.昨天是跟着教程云里雾里的实现了配置,今天再次使用就方便很多. String ...

  8. WebStorm学习笔记001---webstorm-删除项目 两种方法

    webstorm上删除项目,竟然浪费了我十几分钟时间,这让我不得不很气愤的记录下来,以防像我一样的悲催男在webstorm js开发神器上删除项目都找不到. webstorm,首先关闭项目,然后移动到 ...

  9. 链表反转的两种实现方法,后一种击败了100%的用户!

    作者 | 王磊 来源 | Java中文社群(ID:javacn666) 转载请联系授权(微信ID:GG_Stone) 链表反转是一道很基础但又非常热门的算法面试题,它也在<剑指Offer> ...

最新文章

  1. 身边的隐形富豪,都有哪些特征?
  2. 关于《红楼梦》的读后感优秀范文2000字
  3. mysql三范式_MySQL设计之三范式的理解
  4. python数字类型及运算_Python基础教程:运算符以及数据类型解析
  5. postgresql使用pg_rman备份恢复
  6. git上传代码前需要检查什么_肝功能检查前需要做什么准备?这6个要点需做好,以免准确度受影响...
  7. 计算机股票编程,计算机技术《股票软件编程》.doc
  8. 游戏开发需要懂几种语言?
  9. TCP/IP 报文格式(IP数据包、TCP报头、UDP报头)
  10. 梯度消失和梯度爆炸原因,表现,解决方案
  11. 简历中的工作经历要怎么写?
  12. 获取附近小区信息_小程序获取当前位置加搜索附近热门小区及商区的方法
  13. 电磁兼容(EMC):工程师必备之硬件EMC设计规范
  14. CRPR能补偿crosstalk吗?
  15. 解析 | 自动驾驶视觉定位与导航技术的研究与应用
  16. 青少年CTF训练平台Misc-Middle愿风神忽悠你题解
  17. n个点求 能构成多少个三角形
  18. MapperScannerConfigurer和MapperFactoryBean区别
  19. JS数据类型以及存储方式
  20. 关于奈奎斯特判据的通俗理解

热门文章

  1. 【Java 虚拟机原理】Dalvik 虚拟机 ( 打包 Jar 文件和 Dex 文件 | 反编译 Dex 文件 | 分析 Dex 文件反编译结果 )
  2. 【计算机网络】网络层 : 总结 ( 功能 | 数据交换 | IP 数据报 | IPv4 地址 | IPv6 地址 | 路由选择协议 | 路由算法 )★★★
  3. 【数据挖掘】贝叶斯公式应用 拼写纠正示例分析 ( 先验概率 | 似然概率 | 后验概率 )
  4. issubclass和isinstance 反射 内置方法(魔术方法)
  5. Windows 8各个版本的功能详解与对比【中文详表】
  6. DOM-添加元素、节点
  7. AngularJS 服务(Service)
  8. leetcode : Reverse Linked List II [two pointers]
  9. PC问题-该虚拟机似乎正在使用中
  10. (转)CentOS 5.5 64bit 编译安装Adobe FlashMediaServer 3.5