静态Fragment碎片使用 范例
来源:互联网 发布:手机淘宝开店流程 编辑:程序博客网 时间:2024/06/11 00:44
首先,要清楚每一个要是用的fragment的xml布局都要对应一个java'类并且要继承Fragment。
新建两个xml布局
第一个,命名为:layout
<?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:id="@+id/frag1"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="hello 我叫朱萱萱"/></LinearLayout>
第二个xml文件,命名为layout2:
<?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="wrap_content" android:orientation="horizontal" android:id="@+id/frag2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hah woshi 宏伟"/></LinearLayout>分别对应两个Java类,
第一个命名为Frag1的java类:代码如下
package com.example.myapplication;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by 联想 on 2016/6/30. */public class Frag1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.layout,container,false); }}第二个java类,命名为Frag2,代码如下:
package com.example.myapplication;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by 联想 on 2016/6/30. */public class Frag2 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.layout1,container,false); }}
接下来是主布局:
Mainactivity中Java代码:
package com.example.myapplication;import android.app.Activity;import android.os.Bundle;import android.view.Window;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); }}
最后在主布局的activity_main中使用fragment,代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/aaa" android:name="com.example.myapplication.Frag1" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <fragment android:layout_below="@id/aaa" android:id="@+id/id_fragment_content" android:name="com.example.myapplication.Frag2" android:layout_width="fill_parent" android:layout_height="fill_parent" /></RelativeLayout>
注意,《fragment》标签必须有一个name属性,对应返回加载它的java类,而且必须加一个Id,否则程序不知为何老是崩溃。。。
nice,第一个fragment程序创建成功。。。。加油吧少年。。。
1 0
- 静态Fragment碎片使用 范例
- Android之Fragment(一):静态使用碎片
- Fragment碎片的使用
- Fragment 碎片的使用
- Fragment(碎片机制)的静态加载问题
- Android碎片Fragment使用简介
- Android碎片Fragment使用简介
- Fragment碎片化的使用
- Fragment(碎片)的使用实例
- Dynamic Broadcast Receiver in Fragment(在碎片中使用静态的内部内广播)
- Fragment的使用(静态,动态添加碎片;生命周期以及传值)
- 碎片:Fragment
- 碎片Fragment
- Fragment碎片
- 碎片Fragment
- 碎片Fragment
- 碎片---Fragment
- 碎片(fragment)
- 使用命令行将Excel数据表导入Mysql中的方法小结
- Android对外置sd卡的权限问题(上)
- fileZilla错误:无法与 SFTP 服务器建立 FTP 连接
- Android 自定义 spinner (背景、字体颜色)
- CentOS镜像使用
- 静态Fragment碎片使用 范例
- Mysql 配置文件详解
- 多维化计算机数据压缩方法
- 2050折线分割平面(简单递推)(折线切分平面)
- Redis cluster 集群 安装 配置 详解
- windows svn右键清理.svn文件
- 风云办公室
- svm 核函数
- 轻量级数据库 ----SQLite