一.示意图

二.新建一个左侧碎片布局left_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ffff00">

<Button

android:text="加载内容"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/btn"

android:layout_weight="1"

android:onClick="myClick"/>

</LinearLayout>

三.创建左侧碎片类LeftFragment.java:

注意:这里可能会有两个不同包下的Fragment 供你选择,建议使用android.app.Fragment,因为我们的程序是面向Android 4.0 以上系统的,另一个包下的Fragment 主要是用于兼容低版本的Android系统。

public class LeftFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.left_fragment, container, false);

return view;

}

}

四.新建一个右侧碎片布局right_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#0000ff">

<TextView

android:text="TextView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/textView2"

android:layout_weight="1" />

</LinearLayout>

五.新建右侧碎片类RightFragment.java:

public class RightFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.right_fragment, container, false);

return view;

}

}

六.activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>

<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">

<fragment

android:layout_width="0dp"

android:layout_height="match_parent"

android:name="com.example.guo.fragment.LeftFragment"

android:layout_weight="1"

android:id="@+id/left_fragment" />

<fragment

android:layout_width="0dp"

android:layout_height="match_parent"

android:name="com.example.guo.fragment.RightFragment"

android:layout_weight="1"

android:id="@+id/right_fragment" />

</LinearLayout>

注意:对于fragment,在布局文件中,id是必须的,否则会报错

七.新建平板模拟器,运行即可

Fragment简单用法的更多相关文章

  1. Android学习笔记(七)两个Fragment简单跳转示例

    在前两篇博文中分别介绍了Fragment得基础和Fragment的生命周期,然而说了这么多Fragment到底怎么用呢以及我们为什么要使用Fragment?本篇博文将主要探讨这两个问题,首先说下在AP ...

  2. ViewPager的简单用法+适配器+监听器的介绍

    之前的actionbar+fragment文章中写过viewpager的简单用法,但因为是融合的文章,所以今天把viewpager提取出来写了.方便查询浏览~ 思路: 1.在布局文件中设置viewpa ...

  3. CATransition(os开发之画面切换) 的简单用法

    CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...

  4. jquery.validate.js 表单验证简单用法

    引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...

  5. NSCharacterSet 简单用法

    NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...

  6. [转]Valgrind简单用法

    [转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...

  7. Oracle的substr函数简单用法

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  8. Android Fragment 简单实例

    Android上的界面展示都是通过Activity实现的.Activity实在是太经常使用了.我相信大家都已经很熟悉了,这里就不再赘述. 可是Activity也有它的局限性,相同的界面在手机上显示可能 ...

  9. Ext.Net学习笔记19:Ext.Net FormPanel 简单用法

    Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...

随机推荐

  1. Oracle中DBMS_LOB包使用小结

    本文主要介绍oracle数据库中dbms_lob包的使用以及使用dbms_lob包来维护lob数据库类型的基本方法.随着社会的发展,在现代信息系统的开发中,需要存储的已不仅仅是简单的文字信息,同时还包 ...

  2. 1.Centos-Docker安装和使用

    1.安装docker yum install -y epel-release yum install -y docker-io 如果需要安装最新的docker,最好将系统升级到最新版 yum inst ...

  3. python接口自动化7-参数关联【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E ...

  4. 使用bottle进行web开发(1):hello world

    为什么使用bottle?因为简单,就一个py文件,和其他模块没有依赖,3000多行代码. http://www.bottlepy.org/docs/dev/ 既然开始学习,就安装它吧. pip3 in ...

  5. TCP/IP、Http、Socket 简单理解

    转自:http://blog.csdn.net/guyan0319 https://blog.csdn.net/guyan0319/article/details/79404216 一. 什么是TCP ...

  6. myeclipse10.7配置resin4.0.36

    Resin-4.0.35 (built Tue, 12 Feb 2013 10:05:50 PST) Copyright(c) 1998-2012 Caucho Technology.  All ri ...

  7. ubuntu常用终端命令

    # ctrl + l - 清屏 . cLear# ctrl + c - 终止命令. # ctrl + d - 退出 shell,好像也可以表示EOF. # ctrl + z - 将当前进程置于后台,f ...

  8. SQL 并发-转

    脏读.不可重复读  共享锁.悲观锁 和 事务五种隔离级别   一.脏读.不可重复读.幻读1.脏读:脏读就是指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个 ...

  9. Netty源码学习(四)Netty服务器是如何启动的?

    本文会分析Netty服务器的启动过程,采用的范例代码是Netty编写的Echo Server. 0. 声明acceptor与worker 由于Netty采用的reactor模型,所以需要声明两组线程, ...

  10. HDU 2586.How far away ?-在线LCA(ST)-代码很认真的写了注释(捞到变形)

    2018.9.10 0:40 重新敲一遍,然后很认真的写了注释,方便自己和队友看,刚过去的一天的下午打网络赛有一题用到了这个,但是没写注释,队友改板子有点伤,因为我没注释... 以后写博客,代码要写注 ...