静态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