一、 静态加载。
即用布局文件来加载,
1. 主类activity类继承自v4包下的FragmentActivity,
public class MainActivity extends FragmentActivity { public MainActivity() { Log.e("TAG", "MainActivity().."); } @Override protected void onCreate(Bundle savedInstanceState) { Log.e("TAG", "MainActivity onCreate().."); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
2. 定义一个Fragment的子类,并加载一个布局文件。
public class MyFragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载布局得到View对象并返回 //创建一个视图对象, 设置数据并返回 TextView textView = new TextView(getActivity()); textView.setText("fragment11111"); textView.setBackgroundColor(Color.RED); return textView; } }
public class MyFragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载布局得到View对象并返回 //创建一个视图对象, 设置数据并返回 TextView textView = new TextView(getActivity()); textView.setText("fragment22222"); return textView; } }
3. 在布局文件中通过<fragment>指定自定义的Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:name="com.example.l12_fragment1.MyFragment1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <fragment android:name="com.example.l12_fragment1.MyFragment2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> </LinearLayout>
每一个Fragment本质上都是一个Framelayout,它加载的布局为其子布局。
二、动态加载。
用代码来加载,主要API:
FragmentActivity (getSupportFragmentManager)
Fragmentmanager (beginTrasacation() )
FragmentTransacation (add(),replace(),remove(), commit(), addToBackStack() )
--------------------------------------------------------------------------------------------------------------------
public class MainActivity extends FragmentActivity { public MainActivity() { Log.e("TAG", "MainActivity().."); } @Override protected void onCreate(Bundle savedInstanceState) { Log.e("TAG", "MainActivity onCreate().."); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 创建Fragment对象 MyFragment1 fragment1 = new MyFragment1(); // 得到FragmentManager FragmentManager manager = getSupportFragmentManager(); // 得到FragmentTransacation FragmentTransaction transaction = manager.beginTransaction(); // 添加Fragment对象并提交 transaction.add(R.id.ll_main_container, fragment1).commit(); }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="显示fragment2" android:onClick="showFragment2"/> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="移除fragment2" android:onClick="deleteFragment2"/> </LinearLayout> <LinearLayout android:id="@+id/ll_main_container" ////替换的容器 android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> </LinearLayout> </LinearLayout>
public class MyFragment1 extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { Log.e("TAG", "onCreate()"); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.e("TAG", "onCreateView()"); //加载布局得到View对象并返回 //创建一个视图对象, 设置数据并返回 TextView textView = new TextView(getActivity()); textView.setText("fragment11111"); textView.setBackgroundColor(Color.RED); return textView; } }