Fragment的使用

Fragment即碎片,片段的意思。如果有web开发经验的人,比较好理解,把Activity比如为一个html页面,fragment可以看成是一个div,一个一个的嵌在页面中。

本文摘录了一些网上博客的内容,如觉得侵权,请告知,必删除。

(一)使用xml布局文件来创建Fragment

1.先在Activity的布局xml中引入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" >  
  
    <fragment  
        android:id="@+id/id_fragment_title"  
        android:name="com.example.fragementdemo.TitleFragement"  
        android:layout_width="fill_parent"  
        android:layout_height="45dp" />  
  
    <fragment  
        android:layout_below="@id/id_fragment_title"  
        android:id="@+id/id_fragment_content"  
        android:name="com.example.fragementdemo.ContentFragement"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" />  
  
</RelativeLayout>  

使用fragment标签,即可声明,name就是这个fragment对应的类


2.创建类,该类用于创建fragment

public class TitleFragement extends Fragment{
    private ImageButton mLeftMenu;  
	  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState)  
    {  
        View view = inflater.inflate(R.layout.fragment_title, container, false);  
        mLeftMenu = (ImageButton) view.findViewById(R.id.left_btn);  
        mLeftMenu.setOnClickListener(new OnClickListener() {  
            public void onClick(View v)  
            {  
                Toast.makeText(getActivity(),  
                        "i am an ImageButton in TitleFragment ! ",  
                        Toast.LENGTH_SHORT).show();  
            }  
        });  
        return view;  
    }  
}
该类继承Fragment,这里暂时只实现onCreateView方法,类似Activity的onCreate方法


3.创建fragment的布局文件,不然在Fragment类中无法布局,这里以fragment_title为例

<?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:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:gravity="center"  
        android:text="使用Fragment做主面板"  
        android:textSize="20sp"  
        android:textStyle="bold" />  
  
</LinearLayout>  

4.在Activity中只要正常因为Activty的布局文件即可。启动效果如图



(二)动态创建Fragment,在代码中创建

1.创建一个布局xml,里面有一个layout,该layout专门用于放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" 

    android:id="@+id/second_fragment">

</LinearLayout>

 

2.创建fragment的布局xml,表示这个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:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="这是第二个fragment,采用动态添加的方式"/> 

</LinearLayout>

 

3.创建fragment的类,专门用于影射(创建)fragment

public class SecondFragment extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view=inflater.inflate(R.layout.second_fragment,container,false); 

return view;

}

}

 

4.在Activity的类中处理,添加fragment到指定的地方

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.second_activity);

FragmentManager fm=getFragmentManager();

FragmentTransaction fs=fm.beginTransaction();

Fragment fragment=new SecondFragment();

fs.add(R.id.second_fragment,fragment);

fs.commit();

}

 

以上即可动态添加fragment到指定的地方




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值