zoukankan      html  css  js  c++  java
  • Android应用开发学习笔记之Fragment

    作者:刘昊昱 

    博客:http://blog.csdn.net/liuhaoyutz

    Fragment翻译成中文就是“碎片”、“片断”的意思,Fragment通常用来作为一个Activity用户界面的一部分。例如,可以用Fragment1在左边显示一个列表,用Fragment2在右边显示选中列表项的详细内容。两个Fragment属于同一个Activity,并且每个Fragment有它自己的生命周期,可以处理它自己的用户输入事件,另外,Fragment还可以有自己的布局文件。在平板电脑等屏幕比较大的设备上,Fragment比较常用。

    一、静态添加Fragment

    下面我们来看一个使用静态方式添加Fragment的例子,其运行效果如下:

    首先我们创建Fragment1的布局文件,其内容如下:

     

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:text="This is fragment 1" />
    
    </LinearLayout>

    然后创建Fragment2的布局文件,其内容如下:

     

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:text="This is fragment 2" />
    
    </LinearLayout>

    下面创建Fragment1的实现文件,其内容如下:

     

    package com.liuhoayu;
    
    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, container, false);
    	}
    
    }

    下面创建Fragment2的实现文件,其内容如下:

    package com.liuhoayu;
    
    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, container, false);
    	}
    
    }

    接下来实现主布局文件,使用Android:name导入前面创建的Fragment1和Fragment2。

     

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false" >
    
        <fragment
            android:id="@+id/fragment1"
            android:name="com.liuhoayu.Fragment1"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <fragment
            android:id="@+id/fragment2"
            android:name="com.liuhoayu.Fragment2"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
    </LinearLayout>

    最后,实现主Activity文件,其内容如下:

    package com.liuhoayu;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }

    二、动态添加Fragment

    通过上面的例子,我们学会了怎样通过布局文件添加Fragment,下面我们来学习怎样动态的将Fragment添加到Activity中,实际上,动态添加Fragment更加灵活实用。

    修改上一个例子中的主布局文件,将其中添加Fragment的语句去掉,这次我们将动态添加Fragment。修改后的主布局文件内容如下:

     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/main_layout"
        android:orientation="vertical" >
    
    </LinearLayout>

    然后修改主Activity文件,因为没有在布局文件中静态添加Fragment,我们要在这里动态添加,其内容如下:

    package com.liuhaoyu;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Display;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		Display display = getWindowManager().getDefaultDisplay();
    		if (display.getWidth() > display.getHeight()) {
    			Fragment1 fragment1 = new Fragment1();
    			getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();
    		} else {
    			Fragment2 fragment2 = new Fragment2();
    			getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();
    		}
    	}
    
    }

    程序运行效果如下:


  • 相关阅读:
    Network | DNS
    Network | Cookie and Session
    Network | HTTP protocol
    全源最短路径
    最小生成树
    最小生成树
    10. Regular Expression Matching *HARD*
    4. Median of Two Sorted Arrays *HARD* -- 查找两个排序数组的中位数(寻找两个排序数组中第k大的数)
    44. Wildcard Matching *HARD*
    43. Multiply Strings 字符串表示的大数乘法
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3255902.html
Copyright © 2011-2022 走看看