Fragment(碎片)是一种可以嵌入在活动当中的UI片段,它可以让程序更加合理和充分的利用大屏幕的空间。碎片和活动太像了,同样都包含布局,都有自己的声明周期,可以将碎片理解为一种迷你型的活动。

新建FragmentTest项目。假设项目已经建立完成。

新建一个左侧布局left_fragment.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:text="Button"/>
</LinearLayout>

只是放置了一个按钮,并让它水平居中显示,然后:
新建右侧碎片布局right_fragment.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:background="#00ff00"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/>
</LinearLayout>

我们只是将这个布局的背景设置成了绿色,并放置了一个TextView用于显示一段文本。

接着新建一个LeftFragment类,这个类继承自Fragment,这里有两个不同包下的Fragment供你选择,我们要选择support-v4库中的Fragment,因为它可以让碎片在所有Android 版本中保持功能的一致性。另外,我们不需要在build.gradle文件中添加support-v4库的依赖,因为build.gradle文件中已经添加了appcompat-v7库的依赖,而这个库,会将support-v4库也一并引入进来。

现在编写LeftFragment中的代码,如下所示:

package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class LeftFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.left_fragment,container,false);return  view ;}
}

这里仅仅重写了Fragment的onCreata()方法,然后在这个方法中通过LayoutInflater的inflate()方法将刚才定义的left_fragment布局动态加载进来,整个方法就简单明了。

我们再新建一个RightFragment,代码如下:

package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class RightFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.right_fragment,container,false);return  view ;}}

基本山代码都是相同的,接下里修改acitivity_main.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"><fragmentandroid:id="@+id/left_fragment"android:name ="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><fragmentandroid:id="@+id/right_fragment"android:name ="com.example.fragmenttest.RightFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout>

我们使用了<fragment>标签在布局中添加碎片,其中指定的大多数属性都是比较熟悉的,只不过通过android:name属性来显式指明要添加地碎片类名,注意一定要将类的包名也加上。

我们可以运行一下程序:

两个碎片平分了整个活动地布局。

2、动态添加碎片

碎片地真正地强大之处在于,它可以在程序运行时动态地添加到活动当中,根据具体情况来添加碎片,你就可以将程序界面定制的更加多样化。

我们接着上一节的内容,新建another_right_fragment.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:background="#ffff00"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is another right fragment"/></LinearLayout>

这个布局文件和right_fragment.xml中的代码基本相同,只是将背景色变成了黄色。

新建AnotherRightFragment作为另一个右侧碎片,代码如下:

package com.example.fragmenttest;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;/*** Created by ZHJ on 2018/3/6.*/public class AnotherRightFragment extends Fragment {public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){View view = inflater.inflate(R.layout.another_right_fragment,container,false);return  view ;}
}

代码同样简单。在onCreateView()方法中加载了刚刚创建的another_right_fragment布局。这样我们就准备好了另一个碎片,接下来,看一下如何动态将它添加到活动当中,修改activity_main.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"><fragmentandroid:id="@+id/left_fragment"android:name ="com.example.fragmenttest.LeftFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><FrameLayoutandroid:id="@+id/right_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"></FrameLayout></LinearLayout>

现在将右侧碎片替换成一个Fragment中,Fragment是Android中最简单的一种布局,所有控件默认都会摆放在布局的左上角。

由于这里仅仅需要在布局里放入一个碎片,不需要任何定位,非常适合使用FragmentLayout。

我们在代码中向Fragment中添加内容,从而实现动态添加碎片的功能。修改MainActivity中的代码,如下:

package com.example.fragmenttest;import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); Button button =(Button)findViewById(R.id.button);button.setOnClickListener(this);replaceFragment(new RightFragment());} @Overridepublic void onClick(View view) {switch (view.getId()){case R.id.button:replaceFragment(new AnotherRightFragment());break;default:break;}}private void replaceFragment(android.support.v4.app.Fragment fragment){FragmentManager fragmentManager = getSupportFragmentManager();android.support.v4.app.FragmentTransaction tranction = fragmentManager.beginTransaction();tranction.replace(R.id.right_layout,fragment);tranction.commit();}

}

首先我们给左侧碎片中的按钮注册了一个点击事件,然后调用replaceFragment()方法动态的添加Fragment这个碎片。当点击按钮时,又会调用replaceFragment()方法将右侧碎片替换成AnotherRightFragment.结合replaceFramment()方法中的代码可以看出,动态添加碎片主要分为5步。

1、创建待添加的碎片实例

2、创建FragmentManager,在活动中可以直接通过调用getSupportFragmentManager()方法得到。

3、开启一个事务,通过调用beginTransaction方法开启。

4、向容器内添加或替换碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。

5、提交事务,调用commit()方法来完成。

运行一下程序:点击按钮可以看到效果:

Android 第十七课 碎片的简单用法及动态添加碎片相关推荐

  1. Android学习之动态调用碎片

    一.碎片可以在程序运行时动态添加到活动当中.我们可以根据具体情况动态地添加碎片,使得界面定制更加多样化,下面将用个例子来介绍动态调用碎片. 先附上下面例子运行后的效果,效果如下: 点击左边按钮后动态改 ...

  2. Android TabLayout(选项卡布局)简单用法实例分析

    本文实例讲述了Android TabLayout(选项卡布局)简单用法.分享给大家供大家参考,具体如下: 我们在应用viewpager的时候,经常会使用TabPageIndicator来与其配合.达到 ...

  3. 【Android 应用开发】Google 官方 EasyPermissions 权限申请库 ( 最简单用法 | 一行代码搞定权限申请 | 推荐用法 )

    文章目录 一.添加依赖 二.在 AndroidManifest.xml 中配置权限 三.权限申请最简单用法 四.推荐使用的用法 五.GitHub 地址 上一篇博客 [Android 应用开发]Goog ...

  4. 【Android】Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法

    Android适配器之ArrayAdapter.SimpleAdapter和BaseAdapter的简单用法与有用代码片段 ArrayAdapter.SimpleAdapter和BaseAdapter ...

  5. android datepicker 监听,Android编程之DatePicker和TimePicke简单时间监听用法分析

    本文实例讲述了Android编程之DatePicker和TimePicke简单时间监听用法.分享给大家供大家参考,具体如下: DatePicker和TimePicker都是从FrameLayout派生 ...

  6. Android studio游戏开发就是这么简单:卡牌杀系列(核心UI篇:SufaceView的核心用法)

    Android studio游戏开发就是这么简单:卡牌杀系列(核心UI篇:SufaceView的核心用法) Android studio五年经验教你如何:花费了2周快速开发卡牌杀系类游戏,核心思路分享 ...

  7. Android—— ListView 的简单用法及定制ListView界面

    一.ListView的简单用法 2. 训练目标 1) 掌握 ListView 控件的使用 2) 掌握 Adapter 桥梁的作用 实现步骤: 1)首先新建一个项目, 并让ADT 自动帮我们创建好活动. ...

  8. android中checkbox使用方法,Android开发中CheckBox的简单用法示例

    本文实例讲述了Android开发中CheckBox的简单用法.分享给大家供大家参考,具体如下: CheckBox是一种在界面开发中比较常见的控件,Android中UI开发也有CheckBox,简单的说 ...

  9. Android 开发高手课 温故知新篇

    首先推荐大家先阅读<Android 开发高手课>和我之前的三篇练习: Android 开发高手课 课后练习(1 ~ 5) Android 开发高手课 课后练习(6 ~ 8,12,17,19 ...

最新文章

  1. python初学到底怎么学?大神三天快速学习python的方法留下的笔记
  2. javascript高级教程
  3. db2 最近三个月_2020.8.11 腰椎微创三个月后
  4. poj 3522(最小生成树应用)
  5. myeclipse使用git图文教程
  6. request.getAttribute()的数据类型转换问题
  7. 部门年终总结大屏太难做?FVS开发模式用起来,附年终报告模板
  8. 理想传输线终端短路开路和接纯电抗的沿线电压电流分布
  9. 学习nodejs之restful
  10. 无需无线路由,将系统为win7的笔记本变成wifi的方法
  11. 应用程序的可视化图形引擎库控件Vectordraw Developer Framework
  12. 动力节点老杜mysql文件_MySQL/InnoDB数据克隆插件(clone plugin)实现剖析
  13. zznuoj 2174: 水题一发 希望笑纳
  14. qt使用 iostream 头文件
  15. 第三篇:知其然,知其所以然-USB音频设备的开发过程
  16. 咖啡技术培训:传统意式咖啡菜单制作配方及流程
  17. swap函数 交换 vector 里面的两个元素
  18. 从键盘上输入一个整数 N,输出 1~N 之间能被 7 整除的整数的个数,以及这些能被 7 整 除的数的和
  19. 一些恶搞人的C++程序(千万别试,会哭的)
  20. 将写好的java代码打包成jar包并且运行

热门文章

  1. 橱柜高度与身高对照表_下一套房子装修,橱柜就照这样打,布局尺寸这么详细,不信不好用...
  2. superviseddescent (SDM C++11实现)环境配置
  3. 没有与参数列表匹配的 重载函数 strcpy_s 实例_Zemax光学设计实例(84)Ftheta扫描平场透镜的设计...
  4. 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波17 - 锐化高通滤波器 - 梯度图像(罗伯特,Sobel算子)
  5. 学习Python中用numpy与matplotlib遇到的一些数学函数与函数的绘图
  6. robotframework调用python类方法_RobotFramework-调用.py文件
  7. 走过小公司的坑之入职一周
  8. gitbash如何修改可恶的蓝色字体
  9. 常用python分析数据包pipinstallnumpy_安装numpy和matplotlib时,pip依赖关系解析失败
  10. java json删除节点_指定json的某个节点进行增、删、改