Android笔记(十一)第一个Fragment
来源:互联网 发布:itunes软件备份路径 编辑:程序博客网 时间:2024/05/29 12:04
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”不能写错
0 0
- Android笔记(十一)第一个Fragment
- android移植笔记(第一个版本)
- 【十一】Android Fragment
- Android 第一个 train 笔记
- 第一个android客户端笔记
- Android入门学习笔记(三):第一个Android应用程序
- Android学习笔记(三)-第一个Android应用HelloWorld
- android 学习笔记(三) 第一个Android应用HelloWorld
- android学习笔记(一):第一个android程序
- android笔记(2)--第一个android程序
- Android学习笔记(十一)
- android自学笔记 开始--->第一个应用--->第一个项目
- Android学习十一 fragment碎片
- Android学习笔记(三):第一个应用程序
- 构建第一个Android项目 (学习笔记二)
- Android学习笔记(二)第一个Activity
- android学习笔记(2)---第一个And…
- android学习笔记06(第一个程序)
- Excel中高亮一行中最小的数据
- html+vlc 播放多视频
- 基于 Nginx XSendfile + SpringMVC 进行文件下载
- k-medoids(学习Free Mind知识整理)
- URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)
- Android笔记(十一)第一个Fragment
- java quartz的使用,做时间轮询调用 CronTrigger
- CCLabel添加 显示下划线的api
- [LeetCode 222]Count Complete Tree Nodes
- 一个使用watch命令实现的多变量组合测试用例实时输出方案
- 修改postgres编码
- 苹果app审核,如何申请加急审核
- [LeedCode OJ]#14 Longest Common Prefix
- Android基础:Activity的四种启动模式