zoukankan      html  css  js  c++  java
  • Fragment的基本用法

    此处使用android-support-v4.jar。

     一.
    当创建包含Fragment的Activity时,如果用的是Support Library,那么继承的就应该是FragmentActivity而不是Activity。

     二.写Fragment的Layout。
     
     三. 新建ExampleFragment类。onCreateView()方法中指定布局文件。
     public static class ExampleFragment extends Fragment
    {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState)
        {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.example_fragment, container, false);
        }
    }
     三. 通过Activity的布局文件将Fragment加入Activity

          在Activity的布局文件中,将Fragment作为一个子标签加入即可。其中android:name属性是你自己创建的fragment的完整类名。
     <fragment
            android:id="@+id/list"
            android:name="com.yang.tab.Fragment1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

     四.  或通过编程的方式将Fragment加入到一个ViewGroup中

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            
            ExampleFragment fragment = new ExampleFragment();
            fragmentTransaction.add(R.id.linear, fragment);
            fragmentTransaction.commit();

            其中add()方法第一个参数是要插入的父控件组的资源ID。 
  • 相关阅读:
    基于PI的Webservice发布实例
    SM30 表格维护生成器
    各种财务凭证的冲销
    SAP后台作业记录操作
    特性,批次特性建立的BAPI函數
    Windows 上 Nginx 路径的陷阱
    BitKeeper 和 Git
    Javascript 正则验证带 + 号的邮箱地址
    FastAdmin 开发第三天:认识目录
    PHP 中的对象传递
  • 原文地址:https://www.cnblogs.com/yangleda/p/4149427.html
Copyright © 2011-2022 走看看