先上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的更多相关文章

  1. Android入门——UI(8)——Fragment(2)

    先演示一下如何在一个activity中放置两个Fragment,先定义两个Fragment <?xml version="1.0" encoding="utf-8& ...

  2. Android入门——UI(9)

    SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...

  3. android入门——UI(6)——ViewPager+Menu+PopupWindow

    一.使用ViewPager开发新特性引导界面 <?xml version="1.0" encoding="utf-8"?> <Relative ...

  4. android入门——UI(5)

    最近时间实在匆忙,博客的代码基本没有解释. 介绍ExpandableListView <?xml version="1.0" encoding="utf-8&quo ...

  5. android入门——UI(4)

    GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...

  6. android入门——UI(3)

    Spinner控件   ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...

  7. Android入门——UI(2)

    介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...

  8. android入门——UI(1)

    一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...

  9. Android - 用Fragments实现动态UI - 创建Fragment

    你可以把fragment当作activity中的一个活动模块,它有自己的生命周期,自己接收输入消息,可以在activity运行的时候添加和删除(就像可以在其他activity中重用的"子ac ...

随机推荐

  1. web本地存储-IndexedDB

    IndexedDB, HTML5中的一种数据存储方式,索引数据库. 一个网站可能有一个或多个 IndexedDB 数据库,每个数据库必须具有惟一的名称.一个数据库可包含多个对象存储,一个对象存储相当于 ...

  2. (转)iOS开发ARC内存管理技术要点

    转自:http://www.cnblogs.com/flyFreeZn/p/4264220.html 本文来源于我个人的ARC学习笔记,旨在通过简明扼要的方式总结出iOS开发中ARC(Automati ...

  3. 获取枚举Name,Value,Description两种方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  4. poj 2686 Traveling by Stagecoach ---状态压缩DP

    题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马. 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) ...

  5. UINavigationController技巧<一>——修改返回按钮的标题

    UINavigationController 一般push到另一界面后,返回按钮标题便是上一页面的title,但是对于push的第一页或者是上一页面没有title的,返回按钮标题便是默认back,如图 ...

  6. vs2015 不支持javascript的智能提示高亮

    有些人安装了vs2015后发现居然不支持javascrpt的高亮功能,连工具-选项-文本编辑器里面的javascript也没有了,楼主也碰到这么个情况了,估计是有与装了多个版本的原因,楼主电脑安装了V ...

  7. DOM元素对象的属性和方法(2)

    11.contentEditable 作用:设置或返回元素内容可否编辑布尔值,HTML5新增属性 <!DOCTYPE html> <html> <head> < ...

  8. SharePoint webpart中悬浮窗口的webconfig路径

    SharePoint webpart中悬浮窗口的webconfig路径在.../_layouts/15/下.

  9. lightoj 1030

    递推,倒着递推. #include<stdio.h> #define maxn 1010 #define min(a,b) (a)>(b)?(b):(a) int main() { ...

  10. error2

         10:09:40 贾老师,请问这个错在哪里啊! ^^Li_Jia^^ 10:27:41 你第2个for的n是做什么的    10:28:06 换行 ^^Li_Jia^^ 10:28:26 这 ...