学习fragment就要知道怎么去创建一个fragment,创建fragment分为静态创建fragment和动态创建fragment
静态创建Fragment
1.首先创建一个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:id="@+id/text"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="這是一個靜態的fragment" />
</LinearLayout>
2.创建一个类,继承Fragment类,重写onCreateView方法,使用布局填充器获取fragment要显示的布局,这里导包要注意,如果你要导入的包是import android.app.Fragment;那么你的应用只能在android3.0以上的手机上使用。如果你导的包是import android.support.v4.app.Fragment就可以兼容在android3.0以下的手机上,同事关联这个fragment的activity必须继承FragmentActivity这个类,如果不继承这个类就会报AndroidRuntimeException
* E/AndroidRuntime( 5497): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate
a class com.example.work.StaticFragment that is not a Fragment
就是我们创建的那个fragment不是一个fragment,系统不认识这个fragment。
fragment的代码
public class StaticFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout,container,false);
}
}
3.在关联这个fragment的Activity的布局文件中创建一个fragment控件<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.work.MainActivity" >
<fragment android:layout_width="match_parent"
android:id="@+id/static_fragment"
android:name="com.example.work.StaticFragment"
android:layout_height="match_parent"/>
</RelativeLayout>
注意:这个fragment必须有一个name或class属性,指向我们自己的fragment类的全路径(包名+类名)
4.Activity的代码(我们这里什么也没有做)
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
动态的创建fragment
1.创建一个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:id="@+id/text"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是一个动态创建的fragment" />
</LinearLayout>
2.创建一个类,继承Fragment,导包的注意点与静态的是一样的public class StaticFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout,container,false);
}
}
3.先Activity的布局中创建一个存放fragment的容器(空间),我这里使用的是FrameLayout(什么布局均可)<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.work.MainActivity" >
<FrameLayout
android:id="@+id/static_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
注意:这里必须有一个id,我们在activity的java代码中才能取到4.在java代码中动态的添加fragment,我们先通过getFragmentManager获取一个Fragment管理器,通过一个beginTransaction开启一个事务,通过事务添加fragment,执行commit这个事务才会提交。add方法第一个参数是存放fragment的容器,第二个参数是fragment的对象,第三个是tag(标志)这个参数可以有,可以没有,看我们的需要。如果我们以后还需要这个fragment对象,我们可以使用getFragmentManager().findFragmentByTag("dynamicFragment")获取fragment对象
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
DynamicFragment fragment=new DynamicFragment();
transaction.add(R.id.dynamic_fragment, fragment,"dynamicFragment");
transaction.commit();
}
}