Fragment初识
来源:互联网 发布:sql语言属于什么语言 编辑:程序博客网 时间:2024/06/03 14:37
Fragment(碎片)是界面的局部区域,能与用户进行交流,她的生命期与Activity息息相关
分为两类一类是:具有UI界面的,另一类没有UI界面的
具有UI界面的可以直接写在XML,让其加入Activity
<span style="font-size:18px;"><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:id="@+id/myui" tools:context=".Main" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <fragment android:name="com.example.fragmentdemo1.MyFirstFragment" android:id="@+id/myframent_id" android:layout_width="wrap_content" android:layout_height="wrap_content" ></fragment> </LinearLayout></span>
myfirstfragment
<span style="font-size:18px;"><?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_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tomorrow is anther day I LOve you"/> </LinearLayout></span>
MyFirstFragment
<span style="font-size:18px;">package com.example.fragmentdemo1;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;@SuppressLint("NewApi")public class MyFirstFragment extends Fragment{static String tag="MyFirst";@SuppressLint("NewApi")@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view=inflater.inflate(R.layout.myfirstfragment, null);return view;} }</span>
Main
<span style="font-size:18px;">package com.example.fragmentdemo1;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Fragment;import android.app.FragmentTransaction;import android.view.Menu;import android.widget.LinearLayout;public class Main extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//AddFragment();}/* @SuppressLint("NewApi")public void AddFragment(){ Fragment fragment=new MyFirstFragment(); FragmentTransaction transaction=getFragmentManager().beginTransaction(); transaction.add(R.id.myui, fragment, "Love"); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); transaction.commit(); }*/}</span>
上述是具有UI的添加方法,不具有UI的添加方法可以,AddFragment()及去掉注释的哪一种
AddFragment(),首先是创建个Fragment,然后调用getFragment()获得FragmentTransaction对象,并调用beginTransaction 开始执行一个事务,在执行事务过程中,FragmentTransactiond对象的add()将Fragment添加到Activity,add方法的三个参数,分别是Fragment的UI要添加的ViewGroup的节点,Fragment的实例,以及Fragment的tag,tag是没有UI的标识,另外调用setTransition确定Fragment显示过渡方式,最后调用Commit方法来完成此次事务操作。
关于Fragment的生命期,可结合Activity的生命期来学
0 0
- Fragment初识
- Fragment初识
- 初识Fragment
- Fragment初识
- Fragment初识
- 初识Fragment
- Fragment初识
- Learning--初识Fragment Bundles
- Fragment的生命周期初识
- Android 初识Fragment
- Fragment初识(一)
- Fragment初识(二)
- Fragment初识(三)
- Fragment初识(三)
- Fragment初识(四)
- Fragment初识之静态显示
- 初识android——Fragment
- Fragment初识之Fragment管理器(管理多个Fragment)
- 打印出大小为n的数组(可能有重复元素)里所有可能的组合
- java中提示类型不匹配,不能从view转化为boolean
- android Service中启动Dialog
- Android Studio科普篇——1.几个个性化设置
- BZOJ 1098: [POI2007]办公楼biu
- Fragment初识
- 查看系统调用和信号的命令--strace
- wikioi方格取数c
- JAVA——动态代理(在别人的基础上稍作改动)
- 【读书笔记】java语言与面向对象程序设计4:继承与多态
- DOM4J解析XML文档
- Linux常用命令(1):文件目录ln chmod
- Python 使用pycha画图表
- IOS如果将一个十六进制的color转换成UIColor,很实用