fragment的静态加载

fragment是android中一个非常重要的内容,中文译名叫“碎片”或者“片段”,本人喜欢叫他“碎片”,感觉比较有台词的感觉。
现在演示一下fragment的静态加载。

MainActivity.java
是否发现静态加载,不用写什么代码o(^▽^)o;
【注意】fragment不是控件,而是Activity的一部分(即“碎片”),所以不能通过findViewById()的方法获取而是通过findFragmentById()方法或findFragmentByTag()方法获取

获取fragment的方法
1.可以通过getFragmentManager()方法获取FragmentManager对象(这是一个fragment管理者)
2.通过fragmentManager.findFragmentById(R.id.fmid)方法获得MyFragmentone对象(如果fragment标签中设置tag属性,则可以通过fragmentManager.findFragmentByTag()方法获取)

从fragment对象中获取其布局中的控件的方法
1.基于获取fragment的方法获取到fragment对象后,通过MyFragmentone对象的getView()方法获取到其布局fmview;
2,通过fmview的findViewById(R.id.imgid)方法获取ImageView控件,和Activity获取控件的操作相同

package activity.wyc.com.framentdemoone;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import com.wyc.fragment.MyFragmentone;


public class MainActivity extends ActionBarActivity {

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

        FragmentManager fragmentManager = getFragmentManager();
        MyFragmentone myFragmentone = (MyFragmentone)fragmentManager.findFragmentById(R.id.fmid);

        View fmview = myFragmentone.getView();
        ImageView imgObj = (ImageView)fmview.findViewById(R.id.imgid);

    }
}

activity_main.xml
【注意】标签必须写 idname 两个属性,否则无法编译,其中id用于唯一标识,name则指向fragment类;

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.wyc.fragment.MyFragmentone"
        android:id="@+id/fmid" />


</LinearLayout>

以上是Activity及其布局文件,接下来是fragment的java类及其布局文件:

MyFragmentone.java
该类必须继承Fragment类,或其子类,同时必须重写 onCreateView() 的方法
其中LayoutInflater inflater这个参数,可用于动态加载布局文件,作用如下:
View view = inflater.inflate(R.layout.fragment01,null);
该语句的作用类似Activity.java 中的setContentView();
得到view之后,就能通过findViewById()得到你想要的控件
最后将view返回去。

package com.wyc.fragment;

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

import activity.wyc.com.framentdemoone.R;

/**
 * Created by yc on 2015/3/4.
 */
public class MyFragmentone extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment01,null);

        return view;
    }
}

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:id="@+id/imgid" />


</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值