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 ...
随机推荐
- 克鲁斯卡尔(Kruskal)算法
# include <stdio.h> # define MAX_VERTEXES //最大顶点数 # define MAXEDGE //边集数组最大值 # define INFINITY ...
- kubuntu添加windows字体
1.选取字体 在/usr/share/fonts/truetype中新建一个目录,命名为ms,然后将Windows\fonts 目录下的tahoma.ttf.tahomabd.ttf(Tahoma的粗 ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- Docker简单介绍
Docker简单介绍 Docker是一个能够把开发的应用程序非常方便地部署到容器的开源引擎.由Docker公司团队编写,基于Apache 2.0开源授权协议发行.Docker的主要目的例如以下: 提供 ...
- ASP.NET 验证码 不同浏览器 不刷新问题
具体为什么不刷新是缓存机制不同,验证码图片的src或ImageUrl的获取是来自一个文件,由于连接地址没变所以不同内核浏览器有的会认为源没有变,解决办法就是在连接后面加上一个随机参数如可以用JS的Ma ...
- Semantic UI基础使用教程
自己今后要使用Semantic UI进行项目开发了,一步步的记录下来,供大家参考,也让自己去简单的学习一下,有空了就会更新一点东西,大家有什么问题可以相互交流一下,文采不是很好,希望大家要多多见谅,这 ...
- Eclipse 浏览文件插件- OpenExplorer
http://blog.csdn.net/w709854369/article/details/6599167 EasyExplorer 是一个类似于 Windows Explorer的Eclips ...
- C#:占位符的例子
在c#中有两种方式可以输出多个字符. static void Main() { string c=Console.ReadLine(); string d=Console.ReadLine(); Co ...
- CSS中链接文本为图片时的问题(塌陷、对应的图片建立倒角的问题)
我在做Javascript DOM编程艺术的时候,在12章自己做练习时遇到了一个问题,<a>的内容<img>从<a>的盒子中溢出.代码如下: <a href= ...
- windows如何获取Win10 Win8 Win8.1版本
GetVersionEx 在win8 win8.1 win10 之后已经无法使用,如果非要使用的话需要让exe嵌入manifest,mainfest如下.这个文件需要已utf-8存储. <?xm ...