*Fragment是3.0引入的API,主要为了解决平板,大屏幕手机显示问题
*Fragment代表了Activity的子模块,因此可以把fragment理解成Activity的片段
*Fragment必须被嵌入Activity中使用
一般有三个方法要重写
.onCreate()系统创建Fragment的对象后回调该方法,初始化操作 .onCreateView()当fragment绘制界面组件时会回调该方法 .onPause()当用户离开该Fragment时将回调这个方法
*创建一个布局用来填充fragment
<?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:layout_width="match_parent" android:layout_height="wrap_content" android:text="声音大小"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="速度"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3D"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="左声道"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="右声道"/> </LinearLayout>
*创建一个fragement public class SoundFragment extends Fragment {
@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.sound_item,null) ; return view; }
*主界面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layoutwidth="matchparent" android:layoutheight="matchparent" android:orientation="vertical"> <!--容器--> <FrameLayout android:id="@+id/fl_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3"> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:onClick="sound" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="声音"/> </LinearLayout> </LinearLayout>
*Activity
/** * 点击声音按键显示fragment * @param v */ public void sound(View v){ //声明Fragment SoundFragment soundFragment = new SoundFragment(); //获取Fragment管理器 FragmentManager manager = getFragmentManager(); //开启事务 FragmentTransaction transaction = manager.beginTransaction(); //替换掉当前布局中的帧布局容器 transaction.replace(R.id.fl_container,soundFragment); //提交事务 transaction.commit(); }
Activity--------------------Fragment
onAttach()
onCreate()
onCreateView()
onCreate() onActivityCreated
onStart() onStart()
onResume() onResume()
onPause() onPause()
onStop() onStop()
onDestoryView()
onDestory()
onDestory() onDetach()







原文:http://www.cnblogs.com/liunanjava/p/5221745.html