静态创建fragment

导入的包是v4包要向下兼容3.0开始后才有新的fragment
在oncreatView的方法返回对应的view

public class myfragment extends Fragment {
    
    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.dd, container);
        System.out.println("haha");
        
        return inflate;
    }

//在xml中配置fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    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=".MainActivity" >

   <fragment 
       android:name="com.example.fragment.myfragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       
       />

</RelativeLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,660评论 25 709
  • Fragment概述 Fragment是Activity中用户界面的一个行为或者说是一部分。主要是支持大屏幕上动态...
    wangling90阅读 13,967评论 5 75
  • 今天阳光明媚,我努力好好晒晒我自己。 晒晒我的感受,昨天在微社群里我是值班法官,而一天中虽几次记录姐妹们的交作业情...
    心如美玉阅读 2,642评论 1 2
  • 如何上色,有专门的书籍吗,求推荐。一上色全毁。 求大神支招。。。
    _Magnolia__阅读 2,817评论 20 8
  • 很久没有像昨天那么开心了,竟高兴的跑到李先生面前手舞足蹈,着实吓了他一跳。原因其实很简单,只因为自己的文章《愿读书...
    我的春夏秋冬阅读 2,405评论 17 14