先上fragment静态加载的代码

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:text="这是一个Fragment" android:background="#0f0"/> </LinearLayout>

package com.ouc.wkp.ui1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by wkp on 2016/8/25. */ public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_index,container); return view; } }
注意继承的Fragment最好是
import android.support.v4.app.Fragment; 这样可以兼容早期的安卓api

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/fragment1" android:name="com.ouc.wkp.ui1.FragmentIndex" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
注意fragment元素需要指定id

package com.ouc.wkp.ui1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.FragmentActivity; /** * Created by wkp on 2016/8/25. */ public class FragmentDemo extends FragmentActivity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_demo); } }
然后是动态加载

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一个Fragment,通过动态方式加载" /> </LinearLayout>

package com.ouc.wkp.ui1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by wkp on 2016/8/25. */ public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用 @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //别遗忘第三个false参数,否则会出现 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. View view=inflater.inflate(R.layout.fragment_index,container,false); return view; } }
注意代码中的那个注释

<?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"> <Button android:id="@+id/btn_addd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加" /> <Button android:id="@+id/btn_deletee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减少" /> <FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="200dp"></FrameLayout> </LinearLayout>

package com.ouc.wkp.ui1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; /** * Created by wkp on 2016/8/25. */ public class FragmentDemo extends FragmentActivity{ FragmentIndex fragmentIndex; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_demo); fragmentIndex=new FragmentIndex(); findViewById(R.id.btn_addd).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fm=getSupportFragmentManager(); FragmentTransaction ft=fm.beginTransaction(); ft.replace(R.id.frame_layout,fragmentIndex); ft.commit(); } }); findViewById(R.id.btn_deletee).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fm=getSupportFragmentManager(); FragmentTransaction ft=fm.beginTransaction(); ft.remove(fragmentIndex); ft.commit(); } }); } }
效果图 点击增加出现红块 点击减少消失