zoukankan      html  css  js  c++  java
  • 动态创建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.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>

    来看下终于效果:


    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)方法替换内容。

    最后不要忘记提交事务

    看效果:竖直屏幕时:


    然后把手机横放时:



  • 相关阅读:
    实例属性的读取与设置
    淘宝ued
    反射发出动态类型(下)
    iOS多线程的初步研究3
    C# 自动提交到百度ping服务
    .NET自带IOC
    Entity Framework返回IEnumerable还是IQueryable?
    ASP.NET MVC4简单使用ELMAH记录系统日志
    ASP.NET基础之HttpModule学习
    【Linux】Shell学习笔记之四——文件和目录管理(硬连接和软连接)
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5057405.html
Copyright © 2011-2022 走看看