Android 程式开发:(三)初识Fragment —— 3.1简介
来源:互联网 发布:香港风水大师知乎 编辑:程序博客网 时间:2024/05/17 23:11
在之前的教程中,我们已经学习了什么是Activity以及如何使用它。在一个小屏幕的设备上,一个activity通常占据了整个屏幕,同时显示各种UI视图组件。Activity实际上就是视图的容器。然后,当一个activity被显示在一个大屏幕的设备上,例如平板电脑,总会显得有些不适应。因为屏幕太大了,activity中的所有UI组件要充满整个屏幕,这样一来,视图的层次结构就很复杂了。一个更好的办法是使用一种“轻量级”的activity,每个“轻量级”activity包含自己的视图,互不干扰。在运行期间,根据屏幕的方向和尺寸,一个activity可以包含一个或多个“轻量级”activity。在Android3.0以上的版本,这种“轻量级”的activity叫做Fragment.
可以把Fragment想象成Activity的另外一种形式。你创建fragments去包含UI组件,就像创建activities那样。但是,Fragment总是被嵌在Activity中。
1.创建一个名为Fragments的工程。
2.在res/layout文件夹下,新建一个叫fragment1.xml的文件。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00FF00"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/lblFragment1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is fragment #1"
- android:textColor="#000000"
- android:textSize="25sp" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFFE00"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is fragment #2"
- android:textColor="#000000"
- android:textSize="25sp" />
- <Button
- android:id="@+id/btnGetText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="onClick"
- android:text="Get text in Fragment #1"
- android:textColor="#000000" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal" >
- <fragment
- android:id="@+id/fragment1"
- android:name="net.learn2develop.Fragments.Fragment1"
- android:layout_width="0px"
- android:layout_height="match_parent"
- android:layout_weight="1" />
- <fragment
- android:id="@+id/fragment2"
- android:name="net.learn2develop.Fragments.Fragment2"
- android:layout_width="0px"
- android:layout_height="match_parent"
- android:layout_weight="1" />
- </LinearLayout>
6.Fragment1.java中的代码。
- package net.learn2develop.Fragments;
- import android.app.Activity;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment1 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- Log.d("Fragment 1", "onCreateView");
- // ---Inflate the layout for this fragment---
- return inflater.inflate(R.layout.fragment1, container, false);
- }
- }
- package net.learn2develop.Fragments;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Fragment2 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // ---Inflate the layout for this fragment---
- return inflater.inflate(R.layout.fragment2, container, false);
- }
- }
效果图:
- Android 程式开发:(三)初识Fragment —— 3.1简介
- Android 程式开发:(三)初识Fragment —— 3.2动态添加
- Android 程式开发:(三)初识Fragment —— 3.3生命周期
- Android 程式开发:(三)初识Fragment —— 3.4fragments实例之间的通信
- Android 程式开发:(三)碎片简介 —— 3.2 Fragment的生命周期
- Android 程式开发:(三)碎片简介 —— 3.2 Fragment的生命周期
- Fragment初识(三)
- Fragment初识(三)
- 初识android——Fragment
- android开发学习(1)——初识android开发
- Android 程式开发:(一)详解Activity —— 1.1生命周期(前言)
- Android 程式开发:(一)详解Activity —— 1.2设置样式和主题
- Android 程式开发:(一)详解Activity —— 1.3隐藏标题栏
- Android 程式开发:(一)详解Activity —— 1.4显示“普通”对话框
- Android 程式开发:(一)详解Activity —— 1.5浅析Context
- Android 程式开发:(一)详解Activity —— 1.6显示“进度条”对话框
- Android 程式开发:(一)详解Activity —— 1.7显示“稍微复杂”对话框
- Android 程式开发:(二)使用Intents链接Activities —— 2.1基本操作
- Top 150 1.1 unique character
- HTML和 XHTML的最大不同
- Android关于view按键音的修改
- Project Server 2010 沟通管理
- 动态链接库DLL基础编程
- Android 程式开发:(三)初识Fragment —— 3.1简介
- 风格化
- cramfs用于nand flash
- android开发网上的重要知识点
- mysql数据库触发器的操作
- Java线程池
- java常用日期问题集锦
- Class.forName()的作用
- Objective-c 基本概念