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
【注意】标签必须写 id 和 name 两个属性,否则无法编译,其中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>