Fragment的练习

//布局文件的练习

leftFragment_layout.xml    //此布局文件很简单就是一个button按钮

<?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:background="#ff0000">
<Button
    android:id="@+id/btn_run"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:text="this is fragment01"/>
</LinearLayout>
//效果如下
/LeftFragment碎片
package com.example.fragmentdemo;

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

/**
 * Created by Administrator on 2015/8/2.
 */
public class LeftFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //将leftFragment所对应的布局加载到fragment中
       View view=inflater.inflate(R.layout.leftfragment_layout, container, false);
        return view;
    }
}

//RightFragment.xml   // 此布局文件就一个Textview

<?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:background="#00ff00">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="this is textview02"/>

</LinearLayout>
//效果如下


//RightFragment碎片

package com.example.fragmentdemo;

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

/**
 * Created by Administrator on 2015/8/2.
 */
public class RightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.rightfragment_layout,container,false);
        return view;
    }
}

//第3个布局

<?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:background="#ffff00"
    android:orientation="vertical"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dp"
    android:text="another right fragment"/>
</LinearLayout>
//效果图

//实例代码

package com.example.fragmentdemo;

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

/**
 * Created by Administrator on 2015/8/2.
 */
public class AnotherRightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //加载布局文件
        View view=inflater.inflate(R.layout.another_rightfragment_layout,container,false);
        return view;
    }
}
//Activity的布局

<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">

    <fragment
        android:id="@+id/left_fgt"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.example.fragmentdemo.LeftFragment"
        />
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.fragmentdemo.RightFragment"/>
    </FrameLayout>

</LinearLayout>
//Activity示例代码

package com.example.fragmentdemo;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity implements View.OnClickListener {
    //声明控件
    private Button btn_run;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_run = (Button) this.findViewById(R.id.btn_run);
        btn_run.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btn_run:
                //实例化待添加的Fragment
                AnotherRightFragment anotherRightFragment=new AnotherRightFragment();
                //实例化fragmentManager对象
                FragmentManager fragmentManager=getFragmentManager();
                //通过fragment开启事物
               FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
                    //用添加的布局替换 replace  也就是动态的添加布局
                fragmentTransaction.replace(R.id.right_layout,anotherRightFragment);
                //将事物添加到返回栈中
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();//提交
                break;

            default:
                break;
        }


    }
}
//效果图如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值