Android中Fragment碎片运用

        Android在3.0版本引入了Fragment(碎片)功能,它类似于Activity,可以像Activity一样包含布局,通过将Activity 的布局分散到fragment 中,可以在运行时修改activity 的外观,并且由activity 管理的back stack 中保存些变化,很巧妙的解决了不同分辨率手机上UI差异变化的问题。

        Fragment是我们在单个Activity上要切换多个UI界面时,要显示的不同内容。模块化这些UI面板可以提供给其他Acitivity来使用,因此我们可以简单地把Fragment看成类似于TextView控件一样,可以被任意的Activity进行加载。


静态加载:

具体步骤如下:<1>.首先在res->layout下建立两个xml文件。

                         <2>.其次建两个类都继承Fragment,引入上面对应的xml布局文件。

                         <3>.最后就是在activity_main.xml中应用这两个布局文件。

代码如下:

fragment_a.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:background="#00cccc"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/frag1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="FragmentA"
        android:textSize="30sp" />

</LinearLayout>
 fragment_b.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:background="#00FFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/frag2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="FragmentB"
        android:textSize="30sp" />

</LinearLayout>
FragmentA.class

package com.niuml.fragment;

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

/**
 * 导入 android.support.v4.app.Fragment;会报错 java.lang.ClassNotFoundException: 
 * 应该导入android.app.Fragment;这个包
 **/

public class FragmentA extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_a, container, false);
  }

}
FragmentB.class
package com.niuml.fragment;

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

/**
 * 注意:导入 android.support.v4.app.Fragment;会报错 java.lang.ClassNotFoundException:找不到类
 * 应该导入android.app.Fragment;这个包
 **/
public class FragmentB extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_b, container, false);
  }
}

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

    <fragment
        android:id="@+id/fragmenta"
        android:name="com.niuml.fragment.FragmentA"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragmentb"
        android:name="com.niuml.fragment.FragmentB"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout>








    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值