码迷,mamicode.com
首页 > 移动开发 > 详细

android 77 fragment

时间:2015-11-03 08:03:04      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

fragment是3.0之后才有的,之前平板是3.0专用,后来手机和平板都用3.0

 Activity:

package com.itheima.fragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    private Fragment03 fg3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        fg3 = new Fragment03();
        //获取fragment管理器
        FragmentManager fm = getFragmentManager();
        //打开事务
        FragmentTransaction ft = fm.beginTransaction();
        //把内容显示至帧布局
        ft.replace(R.id.fl, fg3);
        //提交
        ft.commit();
    }


    public void click1(View v){
        //把fragment01的界面显示至帧布局中
        //创建fragment对象
        Fragment01 fg1 = new Fragment01();
        //获取fragment管理器
        FragmentManager fm = getFragmentManager();
        //打开事务
        FragmentTransaction ft = fm.beginTransaction();
        //把内容显示至帧布局
        ft.replace(R.id.fl, fg1);
        //提交
        ft.commit();
    }
    
    public void click2(View v){
        //把fragment01的界面显示至帧布局中
        //创建fragment对象
        Fragment02 fg2 = new Fragment02();
        //获取fragment管理器
        FragmentManager fm = getFragmentManager();
        //打开事务
        FragmentTransaction ft = fm.beginTransaction();
        //把内容显示至帧布局
        ft.replace(R.id.fl, fg2);
        //提交
        ft.commit();
    }
    
    public void click3(View v){
        //把fragment01的界面显示至帧布局中
        //获取fragment管理器
        FragmentManager fm = getFragmentManager();
        //打开事务
        FragmentTransaction ft = fm.beginTransaction();
        //把内容显示至帧布局
        ft.replace(R.id.fl, fg3);
        //提交
        ft.commit();
    }
}
<LinearLayout 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" 
    android:orientation="horizontal"
    >

   
    <FrameLayout 
        android:id="@+id/fl"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        ></FrameLayout>
    
     <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fragment01"
            android:onClick="click1"
            />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fragment02"
            android:onClick="click2"
            />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fragment03"
            android:onClick="click3"
            />
    </LinearLayout>
</LinearLayout>

fragment1

package com.itheima.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment01 extends Fragment {

    //返回的view对象会作为fragment01的内容显示在屏幕上
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //inflater是布局填充器
        View v = inflater.inflate(R.layout.fragment01, null);
        return v;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        System.out.println("01create");
    }
    
    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        System.out.println("01start");
    }
    
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        System.out.println("01resume");
    }
    
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        System.out.println("01pause");
    }
    
    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        System.out.println("01stop");
    }
    
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("01destroy");
    }
}
<?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:orientation="vertical" 
    android:background="#ff0000"
    >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="热情的红色"
        android:textSize="20sp"
        />

</LinearLayout>

fragment2

package com.itheima.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment02 extends Fragment {

    //返回的view对象会作为fragment02的内容显示在屏幕上
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment02, null);
        return v;
    }
}
<?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:orientation="vertical" 
    android:background="#0000ff"
    >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="忧桑的蓝色"
        android:textSize="20sp"
        />

</LinearLayout>

fragment3:

package com.itheima.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment03 extends Fragment {

    //返回的view对象会作为fragment03的内容显示在屏幕上
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment03, null);
        return v;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        System.out.println("03create");
    }
    
    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        System.out.println("03start");
    }
    
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        System.out.println("03resume");
    }
    
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        System.out.println("03pause");
    }
    
    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        System.out.println("03stop");
    }
    
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("03destroy");
    }
}
<?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:orientation="vertical" 
    android:background="#00ff00"
    >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小志的绿色"
        android:textSize="20sp"
        />

</LinearLayout>

 

android 77 fragment

标签:

原文地址:http://www.cnblogs.com/yaowen/p/4932050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!