静态展示

注意 静态的开始进入界面的生命周期和动态的不同 详情:14 fragment注意点

  • 步骤一:创建一个类继承 Fragment

    • 代码类型一:

      package com.fmy.demo1;
      
      import android.app.Fragment;
      import android.graphics.Color;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.view.ViewGroup;
      import android.widget.TextView; public class MyFragment extends Fragment {
      private TextView tv; @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      tv = new TextView(getActivity());
      tv.setText("我是一个fragment");
      tv.setOnClickListener(new OnClickListener() { @Override
      public void onClick(View v) {
      tv.setTextColor(Color.rgb((int) (Math.random() * 256), (int) (Math.random() * 256),
      (int) (Math.random() * 256))); }
      });
      return tv;
      }
      }
      • 代码类型二(创建一个view填充):
      package com.fmy.demo2.fragment;
      
      import com.fmy.demo2.R;
      import com.fmy.demo2.UpDate; import android.annotation.TargetApi;
      import android.app.Activity;
      import android.app.Fragment;
      import android.content.Context;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.view.ViewGroup;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;
      import android.widget.Toast; public class MyFragment extends Fragment { private Button bt;
      private EditText et;
      private UpDate activity2; @TargetApi(23)
      @Override
      public void onAttach(Context context) {
      super.onAttach(context);
      activity2 = (UpDate) context;
      //注意API23以下 getContext 空指针
      } @Override
      public void onAttach(Activity activity) {
      // TODO Auto-generated method stub
      super.onAttach(activity);
      activity2 = (UpDate) activity;
      } @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment01, container, false);
      Bundle bundle = getArguments();
      String string = bundle.getString("a");
      TextView tv = (TextView) view.findViewById(R.id.tv);
      et = (EditText) view.findViewById(R.id.et);
      bt = (Button) view.findViewById(R.id.bt);
      bt.setOnClickListener(new OnClickListener() { @Override
      public void onClick(View v) {
      activity2.sendMessage(et.getText().toString());
      }
      });
      tv.setText(string);
      return view;
      }
      }
      • xml文件
      <?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/tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="我是fragment" /> <EditText
      android:id="@+id/et"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="请输入回传" /> <Button
      android:id="@+id/bt"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="回传" /> </LinearLayout>
    • 步骤二 在界面的xml中写入fragment标签

      • id一定要写
      • name指向继承fragment的类
      <LinearLayout 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:orientation="vertical" > <fragment
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/f1"
      android:name="com.fmy.demo2.fragment.MyFragment"
      /> </LinearLayout>

动态创建fragment

  • 步骤一:创建一个类继承 Fragment:

    • 和静态一模一样的 故略
  • 步骤二:在界面的xml设置一容器标签

    • 如下

      <LinearLayout 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:orientation="vertical" > <!--以下作为容器 -->
      <LinearLayout
      android:id="@+id/ll"
      android:layout_width="match_parent"
      android:layout_height="300dp"
      android:orientation="horizontal" >
      </LinearLayout> </LinearLayout>
  • 步骤三:在界面的逻辑代码处填写代码

    • 如下

          //获得fragment管理器
      FragmentManager manager = getFragmentManager();
      //开启事务
      FragmentTransaction tarTransaction = manager.beginTransaction();
      //创建一个fragment对象
      MyFragment fragment = new MyFragment();
      //创建一个bundle 可以用于传值
      Bundle bundle = new Bundle(); //放入数值到bundle
      bundle.putString("a", "aaa");
      //把bundle放入
      fragment.setArguments(bundle);
      //放入fragment
      //第一个参数 布局界面填充容器
      //第二个参数 fragment对象
      tarTransaction.add(R.id.ll,fragment);
      //第二种方法
      //transaction.replace(R.id.ll,fragment);
      //提交事务
      tarTransaction.commit();

14 fragment 创建的更多相关文章

  1. Android 用Fragment创建一个选项卡

    本文结合之前的动态创建fragment来进行一个实践,来实现用Fragment创建一个选项卡 本文地址:http://www.cnblogs.com/wuyudong/p/5898075.html,转 ...

  2. Ubuntu 14 如何创建软件的 启动器/桌面图标?

    如题所示:Ubuntu 14 如何创建软件的 启动器/桌面图标? 解决方案: 将 /usr/share/applications/ 里面的相应图标复制到桌面即可. 同理,也可“拖动”到左边的“启动器栏 ...

  3. 【译】用Fragment创建动态的界面布局(附Android示例代码)

    原文链接:Building a Dynamic UI with Fragments 为了在Android上创建一个动态和多视图的用户界面,你需要封装UI控件和模块化Activity的行为,以便于你能够 ...

  4. 编写利用Fragment创建新闻列表

    编写利用Fragment创建新闻列表 1.创建新闻实体类News,代码如下:   public class News { private String title; private String co ...

  5. 14、创建/恢复ETH钱包身份

    借助网上的一段描述: 若以银行账户为类比,这 5 个词分别对应内容如下: 地址=银行卡号密码=银行卡密码私钥=银行卡号+银行卡密码助记词=银行卡号+银行卡密码Keystore+密码=银行卡号+银行卡密 ...

  6. 14 Fragment 碎片总结

    Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...

  7. 使用Fragment创建灵活的用户界面

      什么是Fragment         Fragment的作用像Activity一样,主要用于呈现用户界面,它依附于Activity存在,但比Activity更灵活. 当我们需要创建动态的,多面板 ...

  8. android 之fragment创建

    1.使用xml标签 1.1定义两个重要属性  <fragment         android:id="@+id/fregment_top"        android: ...

  9. IntelliJ IDEA 14.x 创建工作空间与多个Java Web项目

    以往的Eclipse.NetBeans等开发工具不同,IDEA的Project相当与Eclipse的Workspace,而Module相当于Project. 下边就给出Eclipse与IDEA的概念的 ...

随机推荐

  1. codeforces 523D tatistics of Recompressing Videos

    一个称为DH(DogHouse)的狗的社交网络有k台专用服务器来重新上传可爱的猫的上传视频.每个视频上传后,应该在一个(任何)服务器上重新压缩,之后才可以保存在社交网络中. 我们知道每个服务器需要一秒 ...

  2. ●洛谷P3168 [CQOI2015]任务查询系统

    题链: https://www.luogu.org/problemnew/show/P3168题解: 主席树 强制在线? 那就直接对每一个前缀时间建一个线段树(可持久化线段树),线段树维护优先度权值. ...

  3. ●BZOJ 3796 Mushroom追妹纸

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3796 题解: 题意:    给出三个串 A,B,C    找出一个最长串 S,    使得 ...

  4. UVA 1146 Now or later

    The Terminal Radar Approach CONtrol (TRACON) controls aircraft approaching and departing when they a ...

  5. 习题9-8 Uva1632

    题意: 给你n个宝藏,然后给出他们的位置a[i]以及存在时间tim[i],如果能全部拿完,求出最短时间: 否则输出No solution 思路: 对于一段区间[i,j],你取完之后肯定是在最左端或者最 ...

  6. [bzoj1901]动态区间k大

    定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是多少(1≤k≤j-i+1) ...

  7. xx学院学员评优评奖管理系统

    [勤拂拭软件,软件开发,毕业设计,程序作业,论文写作指导:q-[1215714557]  加好友请注明:勤拂拭)] 之前帮助一个军校学生做的一个评优评奖管理系统,该系统主要用于学校学生评优评先使用. ...

  8. Windows下设置 ssh key,配置GitHub ssh key

    1.新建一个目录,利用git工具打开 Git Bash Here 2.执行如下命令 ssh-keygen -t rsa -C "email@email.com" 其中邮箱为GitH ...

  9. DELL、HP、IBM X86服务器命名规则

    DELL.HP.IBM X86服务器命名规则 各大服务器厂家对于自己的服务器命名都有一定的规则,通常会根据服务器的外观(如塔式.机架式.刀片等).处理器(如Intel或者AMD等).架构等信息来命名. ...

  10. a++与 ++a

    a++先执行表达式再自增执行表达式使用a原值++a先自增再执行表达示执行表达式使用自增a例:int a=0printf("%d",a++); //输0,执行完a=1int a=0p ...