安卓之Fragment详解

来源:互联网 发布:微信公众号淘宝优惠券 编辑:程序博客网 时间:2024/06/05 17:06

两种创建Fragment的方式:

1.在layout中添加<Fragment/>

2.在Activity中动态添加。

在layout中添加<Fragment/>:

在activity_main.xml中添加一个fragment:

<span style="font-family:Microsoft YaHei;"><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <fragment android:name="com.example.fragmenttest.MyFragment"        android:id="@+id/fragment"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>    <LinearLayout        android:id="@+id/linear"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:layout_below="@id/fragment">     <Button        android:id="@+id/btn3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="btn3"              />     </LinearLayout></RelativeLayout></span>

有几点需要注意:

fragment android:name="com.example.fragmenttest.MyFragment"是我们自己建的MyFragment类。

创建一个Fragment和创建一个Activity很类似,继承Fragment类,重写生命周期方法,主要的不同之处就是需要重写一个onCreateView()方法来返回这个Fragment的布局。

<span style="font-family:Microsoft YaHei;">package com.example.fragmenttest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class MyFragment extends android.support.v4.app.Fragment{@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubSystem.out.println("ExampleFragment--onCreateView");        <span style="white-space:pre"></span>return inflater.inflate(R.layout.example_fragment_layout, container, false);}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();}@Overridepublic void onResume() {// TODO Auto-generated method stubsuper.onResume();}@Overridepublic void onStop() {// TODO Auto-generated method stubsuper.onStop();}}</span>

在Activity中动态生成Fragment:

需要注意几点:

1.生成fragment的这个Activity要继承FragmentActivity

2.引入:import android.support.v4这个包里面的内容

3.必须用容器包含fragment

代码如下:

<span style="font-family:Microsoft YaHei;">package com.example.fragmenttest;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.Menu;public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//在程序中加入Fragment        FragmentManager fragmentManager = getSupportFragmentManager();        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();                MyFragment fragment = new MyFragment();        fragmentTransaction.add(R.id.linear, fragment);        fragmentTransaction.commit();}@Overridepublic 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;}}</span>


0 0
原创粉丝点击