FragmentActivity.java
public class FragmentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
Button button = (Button) findViewById(R.id.bt_fragment);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//动态添加fragment
//创建Fragment对象
ContentFragment contentFragment = new ContentFragment();
//创建管理fragment事务的对象
FragmentManager manager = getFragmentManager();
//开启事务
FragmentTransaction beginTransaction = manager.beginTransaction();
//事务添加fragment
beginTransaction.add(R.id.linearLayout, contentFragment);
//提交事务
beginTransaction.commit();
}
});
}
}
ContentFragment.java(ContentFragment.java差不多)
public class ContentFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e("action=", "onCreate");
}
/**
* fragment放布局的方法
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e("action=", "onCreateView");
View view = inflater.inflate(R.layout.fragment_content, container, false);
return view;
}
}
content_activity.xml
<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:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="addFragment"
android:id="@+id/bt_fragment"/>
<!-- 静态方法定义fragment -->
<!-- <fragment -->
<!-- android:layout_width="0dp" -->
<!-- android:layout_height="match_parent" -->
<!-- android:layout_weight="1" -->
<!-- android:id="@+id/contentFragment" -->
<!-- android:name="com.fragment.ContentFragment"/> -->
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/titleFragment"
android:name="com.fragment.TitleFragment"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:id="@+id/linearLayout"></LinearLayout>
</LinearLayout>
fragment_content.xml
<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:orientation="horizontal"
android:background="@android:color/holo_purple">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="content"/>
</LinearLayout>
fragment_title.xml
<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:orientation="horizontal"
android:background="@android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"/>
</LinearLayout>