首页 > 其他 > 详细

Fragment的简单使用和动态添加

时间:2015-01-10 02:06:22      阅读:248      评论:0      收藏:0      [点我收藏+]

?

?一; ?简单Fragment的使用;

? ? 效果图;

??
bubuko.com,布布扣


bubuko.com,布布扣
?
?代码实现:

??

1), 创建fragment1.xml布局

? ?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"   
    android:background="#00FF00">
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #1"
        android:textSize="25sp"
        />
</LinearLayout>

?

2,创建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:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#FF0000"
    >
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #2"
        android:textSize="25sp"
        />
</LinearLayout>

?

3,创建main.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" >
    
   <fragment 
     android:name="com.example.frament.Frament1"  类名的完整地址
     android:id="@+id/frament1"
     android:layout_weight="1"
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"
     />
   
  
   <fragment 
       android:name="com.example.frament.Frament2" 类名的完整地址
       android:id="@+id/fragment2"
       android:layout_weight="1"
       android:layout_height="fill_parent"
       android:layout_width="fill_parent"
       />

</LinearLayout>

?

4,在com.example.frament包下创建Fragment1

package com.example.frament;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class Frament1 extends Fragment {

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.frament1, container,false);
	}
}

?

?

5,在com.example.frament包下创建Fragment2

package com.example.frament;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class Frament2 extends Fragment {

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.frament2, container,false);
	}
}

?

6,创建Activity的类MainActivity1(启动类)?

??

public class MainActivity1 extends Activity {

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

?

?

分析:

使用fragment来添加碎片需要使用一个类继承Fragment

?

public class Frament2 extends Fragment {

在ui中绘制,需要重写onCreateView()方法,该方法返回一个View对象,如下所示;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.frament2, container,false);
	}
使用LayoutInflater 制定布局文件,container参数引用父类 即准备碎片活动 ,savedInstanceState 碎片可以还原到前一次的状态

?

?

?二;Fragment的动态添加

前五步的代码与上面一样

?

6,创建Activity的类MainActivity1(启动类)?

?

package com.example.frament;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Display;

public class MainActivity1 extends Activity {

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.main);
		
		//向活动添加碎片,根据屏幕的纵向和横向显示
		//1,获取碎片管理器
		FragmentManager fragment=getFragmentManager();
		//2,碎片的显示需要使用FragmentTransaction类操作
		FragmentTransaction transacction=fragment.beginTransaction();
		//获取屏幕管理器和默认的显示
		Display display=getWindowManager().getDefaultDisplay();
		//判断横屏
		if(display.getWidth()>display.getHeight()){
			//获取java类
			Frament1 frament1 =  new Frament1();
			transacction.replace(android.R.id.content, frament1);
		}else{
			Frament2 frament2 =  new Frament2();
			transacction.replace(android.R.id.content, frament2);
		}
		//使用FragmentTransaction必须要commit
		transacction.commit();
	}

}

?

分析:

? ? 向活动添加Fragment需要使用FragmentManager类,

? ? ??FragmentManager fragment=getFragmentManager();

?

? ?操作fragment需要使用FragmentTransaction进行碎片的添加,删除等操作?

FragmentTransaction transacction=fragment.beginTransaction();

?

? ?获取手机的屏幕

Display display=getWindowManager().getDefaultDisplay();

?

? 当前页面替换成 frament1

? ?transacction.replace(android.R.id.content, frament1);

?

?
bubuko.com,布布扣
?

横屏时;
bubuko.com,布布扣
?

Fragment的简单使用和动态添加

原文:http://baihe747.iteye.com/blog/2174516

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!