Fragment初识

Fragment(碎片)是界面的局部区域,能与用户进行交流,她的生命期与Activity息息相关

分为两类一类是:具有UI界面的,另一类没有UI界面的

具有UI界面的可以直接写在XML,让其加入Activity

<span style="font-size:18px;"><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:id="@+id/myui"
    tools:context=".Main" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <fragment 
        android:name="com.example.fragmentdemo1.MyFirstFragment"
        android:id="@+id/myframent_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ></fragment> 

</LinearLayout>
</span>

myfirstfragment

<span style="font-size:18px;"><?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" >
    <TextView 
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tomorrow is anther day I LOve you"/>
    

</LinearLayout>
</span>

MyFirstFragment

<span style="font-size:18px;">package com.example.fragmentdemo1;

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

@SuppressLint("NewApi")
public class MyFirstFragment extends Fragment{
	static String tag="MyFirst";

	
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.myfirstfragment, null);
	
		return view;
	}
	
	
	 
	

}
</span>

Main

<span style="font-size:18px;">package com.example.fragmentdemo1;

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

public class Main extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//AddFragment();
	}
/*   @SuppressLint("NewApi")
public void AddFragment(){
	  
	   Fragment fragment=new MyFirstFragment();
	   FragmentTransaction transaction=getFragmentManager().beginTransaction();
	   transaction.add(R.id.myui, fragment, "Love");
	   transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
	   transaction.commit();
	  
   }
*/

}
</span>

上述是具有UI的添加方法,不具有UI的添加方法可以,AddFragment()及去掉注释的哪一种

AddFragment(),首先是创建个Fragment,然后调用getFragment()获得FragmentTransaction对象,并调用beginTransaction 开始执行一个事务,在执行事务过程中,FragmentTransactiond对象的add()将Fragment添加到Activity,add方法的三个参数,分别是Fragment的UI要添加的ViewGroup的节点,Fragment的实例,以及Fragment的tag,tag是没有UI的标识,另外调用setTransition确定Fragment显示过渡方式,最后调用Commit方法来完成此次事务操作。

关于Fragment的生命期,可结合Activity的生命期来学




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值