14 fragment 创建
静态展示
注意 静态的开始进入界面的生命周期和动态的不同 详情: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 创建的更多相关文章
- Android 用Fragment创建一个选项卡
本文结合之前的动态创建fragment来进行一个实践,来实现用Fragment创建一个选项卡 本文地址:http://www.cnblogs.com/wuyudong/p/5898075.html,转 ...
- Ubuntu 14 如何创建软件的 启动器/桌面图标?
如题所示:Ubuntu 14 如何创建软件的 启动器/桌面图标? 解决方案: 将 /usr/share/applications/ 里面的相应图标复制到桌面即可. 同理,也可“拖动”到左边的“启动器栏 ...
- 【译】用Fragment创建动态的界面布局(附Android示例代码)
原文链接:Building a Dynamic UI with Fragments 为了在Android上创建一个动态和多视图的用户界面,你需要封装UI控件和模块化Activity的行为,以便于你能够 ...
- 编写利用Fragment创建新闻列表
编写利用Fragment创建新闻列表 1.创建新闻实体类News,代码如下: public class News { private String title; private String co ...
- 14、创建/恢复ETH钱包身份
借助网上的一段描述: 若以银行账户为类比,这 5 个词分别对应内容如下: 地址=银行卡号密码=银行卡密码私钥=银行卡号+银行卡密码助记词=银行卡号+银行卡密码Keystore+密码=银行卡号+银行卡密 ...
- 14 Fragment 碎片总结
Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...
- 使用Fragment创建灵活的用户界面
什么是Fragment Fragment的作用像Activity一样,主要用于呈现用户界面,它依附于Activity存在,但比Activity更灵活. 当我们需要创建动态的,多面板 ...
- android 之fragment创建
1.使用xml标签 1.1定义两个重要属性 <fragment android:id="@+id/fregment_top" android: ...
- IntelliJ IDEA 14.x 创建工作空间与多个Java Web项目
以往的Eclipse.NetBeans等开发工具不同,IDEA的Project相当与Eclipse的Workspace,而Module相当于Project. 下边就给出Eclipse与IDEA的概念的 ...
随机推荐
- P3928 SAC E#1 - 一道简单题 Sequence2
题目背景 小强和阿米巴是好朋友. 题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强.小强便打算 ...
- [Jsoi2011]分特产
Description JYY 带队参加了若干场ACM/ICPC 比赛,带回了许多土特产,要分给实验室的同学们. JYY 想知道,把这些特产分给N 个同学,一共有多少种不同的分法?当然,JYY 不希望 ...
- ●Codevs 4158 残缺的字符串
题链: http://codevs.cn/problem/4158/ 题解: FFT. 定义两个相同长度的字符串s1,s2的距离为 $$dis(s1,s2)=\sum_{i=0}^{len-1}(s1 ...
- 51nod 1770 数数字
1770 数数字 基准时间限制:1 秒 空间限制:262144 KB 分值: 20 难度:3级算法题 收藏 关注 统计一下 aaa ⋯ aaan个a × b 的结果里面 ...
- hdu 1828 线段树扫描线(周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- bzoj3224Tyvj 1728 普通平衡树 treap
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17706 Solved: 7764[Submit][St ...
- 在QEMU中调试ARM程序【转】
转自:http://linuxeden.com/html/develop/20100820/104409.html 最近我想调试一个运行在QEMU模拟ARM系统中的Linux程序.我碰到过一些麻烦,因 ...
- SparkSQL——用之惜之
SparkSql作为Spark的结构化数据处理模块,提供了非常强大的API,让分析人员用一次,就会为之倾倒,为之着迷,为之至死不渝.在内部,SparkSQL使用额外结构信息来执行额外的优化.在外部,可 ...
- ORACLE 启动过程
1 STARTUP NOMOUNT 1.读取环境变量下dbs目录下的参数文件(spfile/pfile) 查找参数文件的顺序如上面列表的,读取优先级: spfilechongshi.ora > ...
- SpringMvc+Spring+MyBatis 基于注解整合
最近在给学生们讲Spring+Mybatis整合,根据有的学生反映还是基于注解实现整合便于理解,毕竟在先前的工作中团队里还没有人完全舍弃配置文件进行项目开发,由于这两个原因,我索性参考spring官方 ...