关于Android的fragment的使用

fragment的静态使用

首先创建两个fragment,就把fragment当成activity去写布局,第一个是fragment_title:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是标题"
android:textColor="#000000"
android:textSize="25sp" />

</LinearLayout>
然后是内容部分的fragment_content:
<?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:background="#00ff00" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是内容"
android:textColor="#000000"
android:textSize="25sp" />

</LinearLayout>

接着新建两个继承fragment的类(分别是TitleFragment,ContentFragment)重写onCreateView方法以此来加载fragment布局
public class TitleFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
//引入xml布局
return inflater.inflate(R.layout.fragment_title,container,false);
}
}
------------------------------------------------------------------------------------------
public class ContentFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
//引入xml布局
return inflater.inflate(R.layout.fragment_content,container,false);
}
}
然后在主界面activity_main中使用fragment
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<!--name属性就是引入了fragment-->
<fragment
android:id="@+id/fragment_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:name="com.example.von.createfragment01.fragment.TitleFragment"/>
<fragment
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/fragment_title"
android:name="com.example.von.createfragment01.fragment.ContentFragment" />
</RelativeLayout>
从以上代码可以看出,fragment的使用就和其他普通的组件一样,需要注意的是name属性,name就是表示使用到的fragment来自哪里
最后启动主界面,就完成了:
public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }}

fragment的动态使用

 动态使用fragment时,主xml文件中就不用写fragment了,这里创建两个按钮用来调用两个不同的fragment,下面的FrameLayout用来显示fargment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_layout"
    >
    <Button
        android:id="@+id/btn_show_fragment1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示Fragment1"/>

    <Button
        android:id="@+id/btn_show_fragment2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示Fragment2"/>

    <FrameLayout
        android:id="@+id/fragmefnt_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
然后在代码中设置两个按钮的点击事件,点击事件为调用fragmentpublic class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button) findViewById(R.id.btn_show_fragment1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager(); //在Activity中使用Fragment管理器,对所有Fragment进行管理
FragmentTransaction ft
= manager.beginTransaction(); TitleFragment fragmet1 = new TitleFragment(); ft.add(R.id.fragment_container,fragmet1); //add方法:将一个Fragment实例对象添加到集合列表的尾部,当展示的时候会在activity的最上层
ft.commit(); } }); Button button2
= (Button) findViewById(R.id.btn_show_fragment2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction ft = manager.beginTransaction(); ContentFragment fragmet2 = new ContentFragment(); ft.add(R.id.fragment_container,fragmet2); ft.commit(); } }); } }

添加的步骤是这样的:

先使用getSupportFragmentManager方法获取FragmentManager,再用FragmentTransaction开启实务,再通过add方法获取fragment的位置

最后用commit方法启动。

 

猜你喜欢

转载自www.cnblogs.com/vonzc/p/10376652.html