码迷,mamicode.com
首页 > 其他好文 > 详细

动态创建Fragment

时间:2014-05-11 04:08:24      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   class   code   java   

在android3.0之前,每创建一个界面就要新创建一个activity.

在3.0之后引入了Fragment.相当于一个轻量级的activity.不需要在清单文件配置。

先来看下如何创建和使用Fragment :

程序界面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:id="@+id/fragment1"
       android:layout_width="0dip"
       android:layout_weight="1"
       android:layout_height="match_parent"
       android:name="com.example.myframent.Fragment1"
        />
    
    <fragment 
        android:id="@+id/fragment2"
       android:layout_width="0dip"
       android:layout_weight="1"
       android:layout_height="match_parent"
       android:name="com.example.myframent.Fragment2"
        />
</LinearLayout>
这里有两个fragment.(注意到了吗,f是小写的,貌似小写的都不用在清单文件配置)。

每个fragment有一个name节点,这个节点引用了一个java类。看下Fragment1.java:

package com.example.myframent;

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{
	
	//当fragment被创建的时候调用的方法,返回当前fragment显示的内容
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment1, null);
	}
}
在onCreateView里,去填充了一个布局,看下这个布局fragment1.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="#0000ff"
    android:orientation="vertical" >
    

</LinearLayout>

来看下最终效果:
bubuko.com,布布扣

ok.有时候我们有些需求,比如在手机横屏的时候显示一种布局,手机竖凭的时候显示一种布局,或者左边有一个固定的布局,右边的内容是随着左边的操作不停的变。那么该如何操作呢?看下横竖屏幕的这个吧。

看下主界面:

<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"
    
    tools:context=".MainActivity" >

    

</LinearLayout>

这时候主界面里面什么都没有。

看下MainActivity:

package com.example.dynamicfragment;

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

@SuppressLint("NewApi")
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//判断当前手机的朝向
		int width = getWindowManager().getDefaultDisplay().getWidth();
		int height = getWindowManager().getDefaultDisplay().getHeight();
		Fragment fragment1 =new Fragment1();
		Fragment fragment2 = new Fragment2();
		FragmentManager fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		
		if(width>height){
			//水平
			ft.replace(android.R.id.content, fragment1);
		}else{
			ft.replace(android.R.id.content, fragment2);
		}
		ft.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
注意怎么判断横竖屏幕的:如果宽度大于高度肯定是横的屏幕。

这里的Fragment1和上面的Fragment1.java文件是一样的。布局文件也一样。替换的关键是首先动态的创建需要的fragment.然后创建一个FragmentManager。然后开启Fragment事务,然后调用ft.replace(android.R.id.content, fragment1)方法替换内容。最后不要忘记提交事务

看效果:竖直屏幕时:

bubuko.com,布布扣

然后把手机横放时:

bubuko.com,布布扣


动态创建Fragment,布布扣,bubuko.com

动态创建Fragment

标签:android   style   blog   class   code   java   

原文地址:http://blog.csdn.net/howlaa/article/details/25461899

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