Fragment创建

1、静态加载

1、fragment layout
2、fragment类
3、在对应的activity layout中加载 fragment

1、fragment layout

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:text="静态加载"
            android:button="@null"
            android:gravity="center_horizontal"
            android:background="@drawable/radio_pressed"
            android:id="@+id/btn_static"
            />

        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:text="动态加载"
            android:button="@null"
            android:gravity="center_horizontal"
            android:background="@drawable/radio_pressed"
            android:id="@+id/btn_dynamic"
            />

        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:text="生命周期"
            android:button="@null"
            android:gravity="center_horizontal"
            android:background="@drawable/radio_pressed"
            android:id="@+id/btn_life"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_launcher"
            android:text="传递数据"
            android:button="@null"
            android:gravity="center_horizontal"
            android:background="@drawable/radio_pressed"
            android:id="@+id/btn_data"
            />
    </RadioGroup>
</LinearLayout>

2、fragment class

package com.example.administrator.fragment2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.zip.Inflater;

/**
 * Created by Administrator on 2015/7/22.
 */
public class Fragment extends android.app.Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /**
         * 将 layout转成 view对象
         */
        View view = inflater.inflate(R.layout.fragment,container,false);
        return view;
    }
}

3.在对应的activity layout中加载 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" tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/fragment"
        android:name="com.example.administrator.fragment2.Fragment"/>

</RelativeLayout>

2动态加载

前两步和静态加载一样

不同的地方在于

package com.example.administrator.fragment2;

import android.app.*;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //动态加载 fragment
        Fragment fragment = new Fragment(); //这个是我们自己定义的 Fragment
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.addToBackStack(null); //后退按键 允许
        fragmentTransaction.add(R.id.linear,fragment);
        fragmentTransaction.commit();
    }

}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值