码迷,mamicode.com
首页 > 移动开发 > 详细

android Fragment入门

时间:2014-11-03 13:11:28      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   ar   os   使用   

Fragment是android3.0引入的,为什么google推出Fragment呢?主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计。平板电脑的屏幕要比手机的大得多,有更多的空间来放更多的UI组件,并且这些组件之间会产生更多的交互,Fragment允许这样的一种设计,而不需要你亲自来管理 viewhierarchy的复杂变化。 通过将activity的布局分散到fragment中, 你可以在运行时修改activity的外观,可以把Fragment看作是activity界面上的一部分,首先看下图:

bubuko.com,布布扣

bubuko.com,布布扣

第一张图我们看到,点击左边的item跳转到右边的布局上显示,这时候就要启动一个activity,而下面的图点击左边的item,可以在右边显示,用Fragment来显示就行,而不用启动activity,我们知道activity是android的组件,所以它比Fragment占用的内存就大,这就是为什么在大点的屏幕推荐使用Fragment

现在创建一个Android项目Fragment1

activity_main.xml

<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"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
    <fragment
        android:name="com.example.fragment1.Fragment1"
        android:id="@+id/fragment1"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:name="com.example.fragment1.Fragment2"
        android:id="@+id/fragment2"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

在布局中发现一个节点fragment,而我们以前layout中view都是大写字母开头,比如:TextView,所以fragment并不是一个view对象,而是一种类型,android:name指的是Fragment类的全类名,所以Fragment1要继承Fragment对象,如果是使用 android.app.Fragment包下的,那么指定的最小版本必须是11(android:minSdkVersion="11")小于11程序就会报错,因为系统的Fragment是3.0出现的,

Fragment1.java

package com.example.fragment1;


import android.app.Fragment;
import android.os.Bundle;
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) {
		
		return inflater.inflate(R.layout.fragment1, null);
	}
}

fragment1.xml
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#ff0000"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第一个fragment"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>



Fragment2.java

package com.example.fragment1;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		return inflater.inflate(R.layout.fragment2, null);
	}
}

fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#0000ff"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第二个fragment"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java

package com.example.fragment1;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

我们看到MainActivity类中并没有写任何代码,这是静态创建Fragment,效果图:
bubuko.com,布布扣





android Fragment入门

标签:android   style   blog   http   io   color   ar   os   使用   

原文地址:http://blog.csdn.net/coderinchina/article/details/40737757

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!