静态Fragment碎片使用 范例

首先,要清楚每一个要是用的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程序创建成功。。。。加油吧少年。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITzhongzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值