Android入门——UI(7)——Fragment


先上fragment静态加载的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="这是一个Fragment"
android:background="#0f0"/>
</LinearLayout>
fragment_index.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_index,container);
return view;
}
}
FragmentIndex.java
注意继承的Fragment最好是
import android.support.v4.app.Fragment; 这样可以兼容早期的安卓api
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <fragment
android:id="@+id/fragment1"
android:name="com.ouc.wkp.ui1.FragmentIndex"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
fragment_demo.xml
注意fragment元素需要指定id
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentDemo extends FragmentActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_demo);
}
}
FragmentDemo.java
然后是动态加载
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#f00"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个Fragment,通过动态方式加载"
/>
</LinearLayout>
fragment_index.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//别遗忘第三个false参数,否则会出现 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
View view=inflater.inflate(R.layout.fragment_index,container,false);
return view;
}
}
FragmentIndex.java
注意代码中的那个注释
<?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"> <Button
android:id="@+id/btn_addd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" /> <Button
android:id="@+id/btn_deletee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少" /> <FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="200dp"></FrameLayout>
</LinearLayout>
fragment_demo.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentDemo extends FragmentActivity{ FragmentIndex fragmentIndex; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_demo); fragmentIndex=new FragmentIndex(); findViewById(R.id.btn_addd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction(); ft.replace(R.id.frame_layout,fragmentIndex);
ft.commit();
}
}); findViewById(R.id.btn_deletee).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction(); ft.remove(fragmentIndex);
ft.commit();
}
});
}
}
FragmentDemo.java
效果图 点击增加出现红块 点击减少消失

Android入门——UI(7)——Fragment的更多相关文章
- Android入门——UI(8)——Fragment(2)
先演示一下如何在一个activity中放置两个Fragment,先定义两个Fragment <?xml version="1.0" encoding="utf-8& ...
- Android入门——UI(9)
SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...
- android入门——UI(6)——ViewPager+Menu+PopupWindow
一.使用ViewPager开发新特性引导界面 <?xml version="1.0" encoding="utf-8"?> <Relative ...
- android入门——UI(5)
最近时间实在匆忙,博客的代码基本没有解释. 介绍ExpandableListView <?xml version="1.0" encoding="utf-8&quo ...
- android入门——UI(4)
GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- android入门——UI(3)
Spinner控件 ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...
- Android入门——UI(2)
介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...
- android入门——UI(1)
一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...
- Android - 用Fragments实现动态UI - 创建Fragment
你可以把fragment当作activity中的一个活动模块,它有自己的生命周期,自己接收输入消息,可以在activity运行的时候添加和删除(就像可以在其他activity中重用的"子ac ...
随机推荐
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
如何保证服务一直运行?如何保证即使服务挂掉了也能自动重启?在写服务程序时经常会碰到这样的问题.在Linux系统中,强大的shell就可以很灵活的处理这样的事务. 下面的shell通过一个while-d ...
- zip命令
常用示例: (1)zip -r myfile.zip ./* ----将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件, -r表示递归压缩子目录下所有文件. (2)unzip - ...
- linux下创建可引导的U盘系统,使用dd命令进行Linux的ghost
1,通过iso创建可引导的U盘系统. 1.0,格式化U盘为FAT32格式 linux下能够使用命令: mkfs.vfat U盘的设备路径 比如: mkfs.vfat /dev/sdb 当中U盘的路径能 ...
- jQuery特效 隔行变色
1.通过使用onmouseover onmouseout方法 2.变色使用background-color(css)属性 3.变色的标签是td(tr仅仅能使用事件,颜色样式不起作用) 第一种方法 使用 ...
- 密码算法详解——Simon
0 Simon简介 详细文档请直接阅读参考文献[1]. Simon是由NSA设计的轻量级分组密码算法(LIGHTWEIGHT BLOCK CIPHER).主要应用于硬件或软件条件受限(例如:芯片面积要 ...
- dropDownList之"请选择",同时设置默认选项
dropDownList.Items.Insert(0, new ListItem("--请选择--", "-1"));dropDownList.Selecte ...
- Neo4j简介
Neo4j简介 发表于2013年3月16日 11:52 p.m. 位于分类图数据库与图并行计算 现实中很多数据都是用图来表达的,比如社交网络中人与人的关系.地图数据.或是基因信息等等.RDBMS ...
- 转载:C# Office 开发
原文地址:http://blog.sina.com.cn/s/blog_604fb7ae0100x2s7.html 中小企业办公自动化系统都需要有与微软办公软件连接的功能,如把数据导入到电子表格.Wo ...
- 获取checkbox 的选中状态的id、checkbox的一些操作
var id_array=new Array(); $('input[name="id"]:checked').each(function(){ id_array.push($(t ...
- FAQ: Python中if __name__ == '__main__':作用
#hello.pydef sayHello(): str="hello" print(str); if __name__ == "__main__": prin ...