android 动态创建Fragment

前一遍文章我们讲了静态创建Fragment,这个在实际的开发中几乎不用,都是动态创建的,所谓动态创建就是根据某个条件动态创建Fragment,

现在创建一个android项目 dynamicFragment

MainActivity.java

package com.example.dynamicfragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.WindowManager;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		WindowManager wm = getWindowManager();

		int width = wm.getDefaultDisplay().getWidth();
		int height = wm.getDefaultDisplay().getHeight();
		// 1.获取fragment的管理器
		FragmentManager fm = getFragmentManager();
		// 2.管理里面的fragment 开启事务 保证界面更新 同时成功 或者 同时失败
		FragmentTransaction ft = fm.beginTransaction();
		if (height > width) {
			// 竖屏
			// android.R.id.content 代表的是当前的应用的activity
			ft.replace(android.R.id.content, new Fragment1());

		} else {
			// 横屏
			// android.R.id.content 代表的是当前的应用的activity
			ft.replace(android.R.id.content, new Fragment2());
		}
		ft.commit();
	}
}

Fragment1.java

package com.example.dynamicfragment;

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);
	}
}

Fragment2.java

package com.example.dynamicfragment;
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);
	}
}

xml文件就不贴上了,里面就是一个TextView,所以没啥可说的,当屏幕横屏的时候显示Fragment1,屏幕竖屏的时候显示Fragment2

时间: 2024-08-03 13:30:35

android 动态创建Fragment的相关文章

fragment 事务回滚 ---动态创建fragment

1 import java.util.Date; 2 import java.util.LinkedList; 3 4 import com.qianfeng.gp08_day23_fragment5.fragment.TestFragment; 5 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.app.Fragment; 9 import android.app.FragmentTrans

Android动态添加Fragment

Android动态添加Fragment 效果图如下: 项目结构图如下: Fragment1: package com.demo.dongtaifragment; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutI

动态创建Fragment

在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.andro

android动态替换Fragment向下传递数据

以前传递数据都是使用Intent进行传递,但是intent是跳转,我们这个是动态替换 所以刚开始也是一脸懵逼,百度也百度不到,就自己慢慢摸索出来了: 话不多说,直接上代码:(主要核心代码加粗) package com.smartgentechnology;import android.content.Intent;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import

Android 动态创建控件

1 package com.metrox.absolutelayoutdemo; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.Gravity; 6 import android.widget.Button; 7 import android.widget.GridLayout; 8 9 public class MainActivity

实现Android 动态加载APK(Fragment or Activity实现)

尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38565345 最近由于项目太大了,导致编译通不过(Android对一个应用中的方法个数貌似有限制),所以一直琢磨着能否将某些模块的APK不用安装,动态加载,通过在网上查找资料和网友的帮助,终于实现了APK的动态加载,网络上介绍APK动态加载的文章非常多,但是我觉得写得非常好的就是这位大牛的,我基本上就是使用他的这种方案,然后加入了自己的元素.这位大牛是通过Activity实现的,我稍作修改

Fragment解析创建和传参,动态添加fragment

一下是个人的一些总结. 为fragment创建XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <

Android编程动态创建视图View的方法

在Android开 发中,在Activity中关联视图View是一般使用setContentView方法,该方法一种参数是使用XML资源直接创 建:setContentView (int layoutResID),指定layout中的一个XML的ID即可,这种方法简单.另一个方法是 setContentView(android.view.View),参数是指定一个视图View对象,这种方法可以使用自定义的视图类. 在一些场合中,需要对View进行一些定制处理,比如获取到Canvas进行图像绘制,

Android学习路线(二十三)运用Fragment构建动态UI——Fragment间通讯

先占个位置,下次翻译 :p In order to reuse the Fragment UI components, you should build each as a completely self-contained, modular component that defines its own layout and behavior. Once you have defined these reusable Fragments, you can associate them with