- 浏览: 225747 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sfshine:
非常好非常好
Android 窗口管理 -
mthhk:
...
android3.0之Fragment(碎片)基础 -
FrankHB1989:
“C/C++标准不会保证这样的代码一定不会出错”的依据?你要说 ...
Duff's Device -
2006003845:
请问知道 刚体之间怎么不碰撞嘛 ?相互穿插
JBOX2D分析
Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似,下面Android123介绍下Android Fragment的作用和用法。Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多个fragment在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块 ,fragment有自己的生命周期,接收自己的输入事件,你可以添加或移除从运行中的activity.
一个fragment必须总是嵌入在一个activity中,同时fragment的生命周期受activity而影响,举个例子吧,当activity暂停,那么所有在这个activity的fragments将被destroy释放。然而当一个activity在运行比如resume时,你可以单独的操控每个fragment,比如添加或删除。
1,先定义2个Fragment,布局文件R.layout.first&R.layout.second根据自己需求随便写一个,我这里就不贴代码了。
Java代码
import android.app.Fragment;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
public class FirstFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.first, container, false);
registerForContextMenu(root.findViewById(R.id.editText1));
return root;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, 0, Menu.NONE, "菜单1");
menu.add(Menu.NONE, 1, Menu.NONE, "菜单2");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
}
Java代码
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.second, container, false);
}
}
2,在Activity中使用
Java代码
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Honeycomb extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// FirstFragment firstFragment=new FirstFragment();
// //在Activity中通过这个与Fragment通讯
// getFragmentManager().beginTransaction().add(android.R.id.content, firstFragment).commit();
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.btn_1, fm.findFragmentById(R.id.firstFragment));
addShowHideListener(R.id.btn_2, fm.findFragmentById(R.id.secondFragment));
}
void addShowHideListener(int buttonId, final Fragment fragment) {
final Button button = (Button)findViewById(buttonId);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
//为Fragment设置淡入淡出效果
ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
if (fragment.isHidden()) {
ft.show(fragment);
button.setText("隐藏");
} else {
ft.hide(fragment);
button.setText("显示");
}
ft.commit();
}
});
}
}
3,布局R.layout.main中引用碎片
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment android:name="com.ql.app.FirstFragment"
android:id="@+id/firstFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<fragment android:name="com.ql.app.SecondFragment"
android:id="@+id/secondFragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<Button android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐藏"
/>
<Button android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐藏"
/>
</LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
-
- <fragment android:name="com.ql.app.FirstFragment"
- android:id="@+id/firstFragment"
- android:layout_weight="1"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- />
- <fragment android:name="com.ql.app.SecondFragment"
- android:id="@+id/secondFragment"
- android:layout_weight="2"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- />
-
- <Button android:id="@+id/btn_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="隐藏"
- />
- <Button android:id="@+id/btn_2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="隐藏"
- />
- </LinearLayout>
发表评论
-
Java中循环嵌套跳出的高效写法
2014-04-24 19:31 3081(下面写的这些并不是Jdk的新特性,也不是Java某个版本提供 ... -
Android各版本新增功能一览(转)
2013-12-05 20:43 1642Android3.0新增功能:主要增加全息主题 ... -
NotificationManager和Notification的使用总结(转)
2013-11-26 12:51 581这几天一直在修改twig ... -
Android应用程序资源的编译和打包过程分析 (转自老罗的博客)
2013-11-13 10:57 15464我们知道,在一个APK文件中,除了有代码文件之外,还 ... -
Proguard 源码分析 (七) 混淆
2013-11-10 21:19 1763本章我们讲Proguard非常重要的一个步骤:混淆Obfu ... -
Proguard源码分析(六)前文总结
2013-11-05 14:40 1032目前,我们读了Proguard的 ... -
Proguard源码分析(五) ConfigurationParser.keep参数
2013-10-31 14:43 3887本章节我们绕回来讲Keep参数,也就是Configurat ... -
Proguard源码分析(四) 压缩
2013-10-30 10:59 1069上一次我们讲了seed文件,这次我们说压缩,对应的输出文件是 ... -
Proguard源码分析(三)Seed文件
2013-10-29 12:45 1477Seed文件就是保持住的类文件,直白一点就是不被混淆的文件, ... -
Proguard源码分析(二)输出文件
2013-10-28 10:28 869ProGuard outputs thefollowing ... -
Android 2.2.2到Android 4.2.2源码下载地址(转)
2013-10-28 10:13 948Android 2.2.2到Android 4.2.2源码下 ... -
代码混淆器Proguard源码分析(一) 读取
2013-10-22 19:40 2259Proguard是Android中经常用的混淆工具,当然你也 ... -
Android内存之VSS/RSS/PSS/USS
2013-09-25 14:07 655Terms VSS - Vi ... -
chrome开源工程(转)
2013-09-13 10:42 1273在chrome地址栏输入about:credits就可以看 ... -
dex文件结构(转)
2013-09-03 14:10 970Dex文件和Dalvik虚拟机 在Android系统中 ... -
android clipPath切割画布
2013-08-29 12:00 6176(转自:http://wallage.blog.163.co ... -
Android WebView控件
2013-06-06 11:46 2207android.webkit库聚合了webkit内核的浏览器 ... -
dumpsys命令~(非常有用~)
2013-04-03 12:02 0dumpsys是系统中重要的函数,我们来看看它如何使用,都能 ... -
内存监控命令~
2013-04-03 00:30 0meminfo 命令:cat /proc/meminfo ... -
adb常用命令
2013-04-03 00:17 1048Android 调试桥(adb)是多种用途的工具,该工具可以 ...
相关推荐
Fragment的概念是从Android3.0开始引入的,直译为碎片、片段,目的是为不同屏幕大小的设备(手机、平板等)创建灵活动态的UI。诚如其名,你可以把Fragment当作是Activity的模块化组件,它拥有自己的生命周期和UI,接受...
android3.0以上常用的碎片技术fragment,分享大家学习中
为了让界面可以在平板上更好地展示,Android 在3.0版本引入了Fragment(碎片)功能,它非常类似于 Activity,可以像Activity 一样包含布局。Fragment 通常是嵌套在Activity 中使用的,现在想象这种场景: 有两个...
Fragment,碎片,是Android 3.0之后加入的一个非常重要的概念。每个Fragment都有相应的Activity对它进行托管。一个Activity中可以有多个Fragment,这很自然的给大屏幕的适配提供了很便捷的方案。现在大家在开发中都必...
Fragment是Android3.0新增的概念,中文意思是碎片,它与Activity十分相似,用来在一个 Activity中描述一些行为或一部分用户界面.使用多个Fragment可以在一个单独的Activity中建 立多个UI面板,也可以在多个Activity中...
Fragment和ActionBar都是Android3.0之后出现的,Fragment,碎片,主要是为了支持更多的UI设计在大屏幕设备上,如平板。因为现在设备的屏幕越来越大,使用Fragment可以更灵活的管理视图层次的变化。像Activity一样,...
Fragment是Android3.0新增的概念,是碎片的意思,它和Activity很相像
Fragment(碎片)是一种可以嵌入在Activity中的UI片段,与Activity非常相似,不仅包含布局,同时也具有自己的生命周期 Fragment不能独立存在必须嵌入到Activity中使用,所以Fragment生命周期直接受所在的Activity...
android 3.0版本提供的 新技术 碎片 ,本程序提供 fragment 一般的使用方法
Fragment是Android 3.0的时候被引入的,主要目的是为了给大屏幕(如平板电脑)添加动态和灵活的UI支持。利用Fragment实现更好的用户体验。 Fragment加载 1.静态加载:添加Fragment到Activity布局中,以xml的形式。 2....
为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像Activity一样包含布局。Fragment通常是嵌套在Activity中使用的,现在想象这种场景:有两个Fragment,...
在Android3.0之后,谷歌推出了Fragment,Fragment在Android中被称为碎片。 我们可以把Fragment看作是Activity的一个界面或者组成部分,而且Fragment具有与Activity很相似的生命周期,我们可以在Activity中动态的...
自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。其目的是为了解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件...
Fragment是Android honeycomb 3.0开始新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法。Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多...
自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。下面这篇文章主要给大家分享了关于Android中你可能不知道的Fragment妙用,对大家具有一定的参考学习价值,需要的朋友可以参考下。
Android安全开发基础: 图形界面(UI)和碎片(Fragment)(上) Android安全开发基础: 图形界面(UI)和碎片(Fragment)(下) Android安全开发基础: 持久化技术 Android安全开发基础: 多媒体 Android安全开发基础:...
Android安全开发基础--10--图形界面(UI)和碎片(Fragment)(上).pdf Android安全开发基础--11--图形界面(UI)和碎片(Fragment)(下).pdf Android安全开发基础--12--持久化技术.pdf Android安全开发基础--13-...
Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,...
1.5.3 Android基础组件 1.5.4 高级UI概念 1.5.5 Android Service组件 1.5.6 Android媒体和电话组件 1.5.7 Android Java包 1.6 利用Android源代码 1.7 本书的示例项目 1.8 小结 第2章 设置...