Fragment是碎片的意思,能够參照Activity来理解Fragment,由于它们都能包括布局,都有自己的生命周期。

以下我们要让主活动包括两个碎片,而且让两个碎片充满屏幕

1.首先,新建两个碎片的布局文件

left.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="vertical" > <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" /> </LinearLayout>

right.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:background="#00ff00"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="right"/> </LinearLayout>

2.接着,新建一个 LeftFragment 类和RightFragment类,继承自 Fragment。

public class LeftFragment extends Fragment {

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.left, container,false);
return view;
} }
public class RightFragment extends Fragment {

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.right, container,false);
return view;
} }

3.最后。改动 activity_main.xml中的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <fragment
android:id="@+id/left_fragment"
android:name="com.example.fragment.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/right_fragment"
android:name="com.example.fragment.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>

4.效果例如以下图所看到的



5.注意事项:

当时新建 LeftFragment 类和RightFragment类的时候没有把返回值改动为return view;

android:name=”com.example.fragment.LeftFragment”不能写错

Android笔记(十一)第一个Fragment的更多相关文章

  1. Android笔记(六十八) Fragment总结

    Fragment的产生: 为了适应各种尺寸的屏幕,谷歌推出Fragment,可以把Fragment成Activity的一个组成部分,它拥有自己的生命周期.可以接收并处理用户的各种事件,还可以动态的增删 ...

  2. Android笔记:如何在Fragment里使用findViewById()方法?

    在Activity中,可以直接用findViewById(int id),通过xml的id来找到对应的View. 查找官方的api,具体如下: https://developer.android.go ...

  3. Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment

    你能够把fragment看成是activity的模块化部分.它拥有自己的生命周期,接受它自己的输入事件,你能够在activity执行时加入或者删除它(有点像是一个"子activity&quo ...

  4. 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)

    即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...

  5. Android学习笔记(五)Fragment简介

    Fragment是在Android 3.0 (API level 11)中引入的Activity的子模块.初衷是为了适应大屏幕的平板电脑,我们只需要使用Fragment对UI组件进行分组.模块化管理, ...

  6. Android仿微信界面--使用Fragment实现(慕课网笔记)

    1 效果图  这里我们没有实现滑动切换view的功能 2 具体实现: 2.1 布局文件:top.xml, bottom.xml,tab01.xml,tab02.xml,tab03.xml,tab04. ...

  7. 【Android Developers Training】 20. 创建一个Fragment

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. Android 笔记-Fragment 与 Activity之间传递数据

    Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...

  9. Android笔记(十九) Android中的Fragment

    通常我们使用Activity来展示界面,但是在手机上界面可能显示的很好看,但在平板上,因为平板的屏幕非常大,手机的界面放在平板上可能会出现控件被拉长.控件之间间距变大等问题.为了更好的体验效果,在Ac ...

  10. Android从一个Fragment跳转到另一个Fragment后原来的组件不消失

    问题描述 Activity上放置了一个Fragment,Fragment上有按钮,点了按钮后,应该跳转到另一个Fragment, but 原来的Fragment的按钮不会消失,新的Fragment不是 ...

随机推荐

  1. 【asp.net Core 2.0 初步探索】

    首先下载 对应的SDK 和runtime https://www.microsoft.com/net/core#linuxubuntu           ---------当前为 1.1 稳定版本 ...

  2. std::copy 和 std::back_inserter

    #define print_vector(v1) \ for(auto iter = v1.begin();iter != v1.end();iter++) \ cout<<*iter&l ...

  3. Python实现天数倒计时计算

    tips:在datetime模块里有一个计算时间差的 timedelta.让两个datetime对象相减就得到timedelta ###--Python实现天数倒计时计算 #tips:在datetim ...

  4. Python 绘图库的使用:matplotlib

    Matplotlib 官方API地址:https://matplotlib.org/ 例子: import matplotlib.pyplot as plt num_list=[1.5,0.6,7.8 ...

  5. 【LeetCode】132. Palindrome Partitioning II

    Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition ...

  6. php 内存管理

    内存是计算机⾮常关键的部件之⼀,是暂时存储程序以及数据的空间,CPU只有有限的寄存器可以⽤于存储计算数据,⽽⼤部分的数据都是存储在内存中的,程序运⾏都是在内存中进⾏的.和CPU计算能⼒⼀样, 内存也是 ...

  7. 如何理解并学习javascript中的面向对象(OOP) [转]

    如果你想让你的javascript代码变得更加优美,性能更加卓越.或者,你想像jQuery的作者一样,写出属于自己优秀的类库(哪怕是基于 jquery的插件).那么,你请务必要学习javascript ...

  8. windows平台安装redis服务

    有时候我们需要在windows平台上使用redis作为缓存服务,这个时候就需要将redis安装为window服务. 现在将我安装过程记录下来,方便自己或者有类似需求的同学参考.主要是参考网上现有的例子 ...

  9. if语句的数据驱动优化(Java版)

    举个栗子,如果我要输出数字对应的中文描述,我可以用这种方法来写: int num=2; if (num==1){ System.out.println("一"); } else i ...

  10. unity5 Orthographic模式相机视截体尺寸计算

    一,通过编辑器中数值计算. 如图,相机为Orthographic模式,其camera size为5.57,是什么含义呢,经过测量,发现视图中视截体的高度是5.57x2. 那么视截体的宽度怎么决定呢? ...