Fragment初识

来源:互联网 发布:sql语言属于什么语言 编辑:程序博客网 时间:2024/06/03 14:37

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")@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic 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 {@Overrideprotected 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
原创粉丝点击