在一个Activity中添加动态的Fragment
public class MainActivity extends FragmentActivity implements View.OnClickListener { FragmentManager manager; FragmentTransaction transaction; Fragment fragment[]; HomeFragment f1; ContFragment f2; ProblemFragmet f3; StowFragment f4; PersonalFragment f5; Button btn_1; Button btn_2; Button btn_3; Button btn_4; Button btn_5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_1 = (Button) findViewById(R.id.btn_one); btn_2 = (Button) findViewById(R.id.btn_two); btn_3 = (Button) findViewById(R.id.btn_three); btn_4 = (Button) findViewById(R.id.btn_four); btn_5 = (Button) findViewById(R.id.btn_five); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); buildFragmentView(); } private void buildFragmentView() { manager = getSupportFragmentManager(); transaction = manager.beginTransaction(); fragment = new Fragment[5]; f1 = new HomeFragment(); fragment[0] = f1; transaction.add(R.id.fra_one, fragment[0], "HomeFragment"); transaction.commit(); } public void onClick(View v) { transaction = manager.beginTransaction(); switch (v.getId()) { case R.id.btn_one: btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_press_down); btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_button); btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_button); btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_button); btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_button); transaction.replace(R.id.fra_one, fragment[0], "HomeFragment"); break; case R.id.btn_two: btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_button); btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_press_down); btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_button); btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_button); btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_button); if (fragment[1] == null) { f2 = new ContFragment(); fragment[1] = f2; } transaction.replace(R.id.fra_one, fragment[1], "ContFragment"); break; case R.id.btn_three: btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_button); btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_button); btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_press_down); btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_button); btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_button); if (fragment[2] == null) { f3 = new ProblemFragmet(); fragment[2] = f3; } transaction.replace(R.id.fra_one, fragment[2], "ProblemFragmet"); break; case R.id.btn_four: btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_button); btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_button); btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_button); btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_press_down); btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_button); if (fragment[3] == null) { f4 = new StowFragment(); fragment[3] = f4; } transaction.replace(R.id.fra_one, fragment[3], "StowFragment"); break; case R.id.btn_five: btn_1.setBackgroundResource(R.mipmap.mobile_one_home_page_button); btn_2.setBackgroundResource(R.mipmap.mobile_one_content_page_button); btn_3.setBackgroundResource(R.mipmap.mobile_one_question_page_button); btn_4.setBackgroundResource(R.mipmap.mobile_one_stow_page_button); btn_5.setBackgroundResource(R.mipmap.mobile_one_details_page_press_down); if (fragment[4] == null) { f5 = new PersonalFragment(); fragment[4] = f5; } transaction.replace(R.id.fra_one, fragment[4], "PersonalFragment"); break; } transaction.commit(); } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <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="vertical" android:gravity="bottom" tools:context=".MainActivity"> <FrameLayout android:id="@+id/fra_one" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:layout_margin="1dp" android:background="@drawable/aa" android:orientation="horizontal"> <Button android:layout_weight="1" android:id="@+id/btn_one" android:layout_width="0dp" android:layout_height="match_parent" android:background="@mipmap/sy" /> <Button android:layout_weight="1" android:id="@+id/btn_two" android:layout_width="0dp" android:layout_height="match_parent" android:background="@mipmap/wz" /> <Button android:layout_weight="1" android:id="@+id/btn_three" android:layout_width="0dp" android:layout_height="match_parent" android:background="@mipmap/wt" /> <Button android:layout_weight="1" android:id="@+id/btn_four" android:layout_width="0dp" android:layout_height="match_parent" android:background="@mipmap/dx" /> <Button android:layout_weight="1" android:id="@+id/btn_five" android:layout_width="0dp" android:layout_height="match_parent" android:background="@mipmap/gr" /> </LinearLayout> </LinearLayout>