【Android】50、碎片的简单用法
来源:互联网 发布:软件市场营销方案 编辑:程序博客网 时间:2024/06/06 12:42
本篇博文最后修改时间:2016年8月31日,11:09。
一、简介
本篇介绍碎片的简单用法。
二、实验平台
系统版本:Windows7 家庭普通版 32位操作系统。
三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.csdn.net/omoiato
联系方式:315878825@qq.com
Java零基础入门交流群:541462902
四、碎片的使用方式
1、创建一个平板模拟器
碎片通常都是在平板开发中才会使用的,因此我们首先要做的就是新建一个平板电脑的模拟器。
由于4.0 系统的平板模拟器好像存在bug,这里就新建一个4.2 系统的平板模拟器。
2、新建一个FragmentTest 项目
在一个活动当中添加两个碎片,并让这两个碎片平分活动空间。
新建一个左侧碎片布局left_fragment.xml,代码如下所示:
<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/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Button" /></LinearLayout>
这个布局非常简单,只放置了一个按钮,并让它水平居中显示。
3、新建右侧碎片布局right_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20sp" android:text="This is right fragment" /></LinearLayout>
我们将这个布局的背景色设置成绿色,并放置了一个TextView 用于显示一段文本。
4、新建一个LeftFragment 类,继承自Fragment
注意,这里可能会有两个不同包下的Fragment 供你选择,
建议使用android.app.Fragment,因为我们的程序是面向Android 4.0以上系统的,
另一个包下的Fragment 主要是用于兼容低版本的Android 系统。
LeftFragment的代码如下所示:
public class LeftFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment, container, false); return view; }}
这里仅仅是重写了Fragment 的onCreateView()方法,
然后在这个方法中通过LayoutInflater的inflate()方法将刚才定义的left_fragment 布局动态加载进来,
整个方法简单明了。
5、用同样的方法再新建一个RightFragment
public class RightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_fragment, container, false); return view; }}
基本上代码都是相同的,相信已经没有必要再做什么解释了。
6、修改activity_main.xml中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/left_fragment" android:name="com.example.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/right_fragment" android:name="com.example.fragmenttest.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /></LinearLayout>
可以看到,我们使用了<fragment>标签在布局中添加碎片,其中指定的大多数属性都是熟悉的,
只不过这里还需要通过android:name 属性来显式指明要添加的碎片类名,注意一定要将类的包名也加上。
这样最简单的碎片示例就已经写好了。
7、运行程序
效果如图。
正如我们所期待的一样,两个碎片平分了整个活动的布局。
- 【Android】50、碎片的简单用法
- 动态加载碎片的简单用法
- 碎片的简单用法及碎片与Activity之间的通信
- Android Fragment 碎片 简单代码
- Android 关于ViewPager结合碎片Fragment的简单使用
- android:碎片的生命周期
- Android---碎片的生命周期
- Android学习之简单地使用碎片
- 【Android】21、碎片的生命周期
- Android碎片的小秘密
- Android碎片(Fragment)的创建
- android:碎片的使用方式
- Android:使用fragment(碎片)—让你的应用更灵活(一)简单运用
- Android进阶之路 - Fragment(碎片化)、进栈、不进栈的简单使用(一)
- 简单了解文档碎片DocumentFragment的使用
- 探究碎片--简单的使用方式
- [Android]简单Spinner的用法
- android 简单tabHost的用法
- (LeetCode)Intersection of Two Linked Lists --- 两个链表的交集
- mysql left on 和where中执行顺序
- Java 7之基础 - 强引用、弱引用、软引用、虚引用
- 为MySQL数据库建立索引
- 闭包学习
- 【Android】50、碎片的简单用法
- hdu2256Problem of Precision+矩阵快速幂+广义斐波拉契
- C++实现链表
- hadoop启动错误:Path /XXX should be specified as a URI in configuration file
- 大数据入门,这10点你是必须要知道的
- linux上创建线程的类
- 金蝶打开软件时提示以独占的方式被打开
- block,inline和inline-block概念和区别
- sqlmap用户手册