Fragment(1)
来源:互联网 发布:淘宝网上买电视可靠吗 编辑:程序博客网 时间:2024/06/08 14:02
创建一个Fragment类
兼容的Fragmgent要用到v4包或者是v7包也行。
需要继承于Fragment类,重写主要的生命周期函数。一个不同的是,我们必须使用onCreateView()这个回调方法来定义我们的layout(布局)。
简单的例子:
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.ViewGroup;public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false); }}
使用XML添加一个Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout>在FragmentActivity中的代码:
import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); }}
如果用的是v7包的话,就要用ActionBarActivity而不是FragmentActivity了。
!!!当我们在XML布局中添加一个Framgment,就不可能在运行时移除这个Fragment。如果想要在与用户交互的时候动态的换入换出,我们一定要在Activity第一次启动的时候添加它。
0 0
- Fragment(1):基本概念
- Fragment(1)
- Fragment详解(1)
- Fragment使用(1)
- [Fragment-1]Fragment生命周期
- Fragment详解之三——管理Fragment(1)
- Fragment详解之三——管理Fragment(1)
- Fragment详解之三——管理Fragment(1)
- Fragment详解之三——管理Fragment(1)
- Fragment详解之三——管理Fragment(1)
- Fragment详解之三——管理Fragment(1)
- Fragment详解之三——管理Fragment(1)
- Fragment详解之三——管理Fragment(1)
- 初入Fragment(1)——静态加载fragment
- Android Fragment 小结(1)
- fragment(1)
- Fragment (1)
- Fragment---1
- myeclipse10配置hiberate
- 面向过程和面向对象的区别
- 单链表实现栈
- 芯片加密技术浅谈
- Android Application-Telephony-RIL 2 PhoneApp
- Fragment(1)
- 《老罗的Android之旅》阅读笔记——Content Provider
- MVC与ASP.NET的区别
- MapReduce Design Patterns(4、分箱、全排序、混洗)(八)
- Android kernel Crash后,定位出错点的方法
- PHP安全设置之PHP.ini
- http://mobile.51cto.com/iphone-280965.htm
- 精通CSS+DIV网页样式与布局--图片效果
- Android Add外部library工程,总是链接失败的原因