Fragment的使用

今天我们讲下Fragment,其使用方法比较简单,但不是很好描述,我们直接看代码吧。

MainActivity:

package com.example.fragment_02;

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

public class MainActivity extends Activity {
	//Fragment管家
	private FragmentManager manager;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//碎片管理器,用于管理Activity和Fragment之间的交互
		manager = getFragmentManager();
		
		//开启了Fragment的事务,方法:add()添加 	replace()替换		remove()移除
		FragmentTransaction transaction = manager.beginTransaction();
		
		//将Fragment添加到指定容器当中
		transaction.add(R.id.ll_left, new LeftFragment());
		transaction.add(R.id.ll_right, new RightFragment());
		
		//提交事务
		transaction.commit();
		
	}

	

}
左边的Fragment:

package com.example.fragment_02;

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

@SuppressLint("NewApi")
public class LeftFragment extends Fragment{
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		return inflater.inflate(R.layout.left, null);
	}
}
右边的Fragment:

package com.example.fragment_02;

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

@SuppressLint("NewApi")
public class RightFragment extends Fragment{
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		return inflater.inflate(R.layout.right, null);
	}
	
}
main_activity.xml文件:

<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"
    android:orientation="horizontal" >

    <LinearLayout 
        android:id="@+id/ll_left"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#f00">
        
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/ll_right"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#0ff">
        
    </LinearLayout>

</LinearLayout>
左边的xml文件:

<?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="#f00">
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="左边"
        android:textSize="20sp"/>

</LinearLayout>
右边的xml文件:

<?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="#0ff">
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="右边"
        android:textSize="20sp"/>

</LinearLayout>
运行结果:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值