Fragment的知识概要
1,Fragment可以作为Activity的界面的一部分组成出现
2,可以在一个Activity中同时出现多个Fragment,并且一个Fragment可以在多个Activity中使用
3, 在Activity的运行过程中,可以添加,移除或者替换Fragment
4,Fragment可以响应自己的输入事件,并且有自己的生命周期,Fragment的生命周期会受宿主Activity的生命周期的影响
注意:
在Activity的layout文件中声明Fragment时,需要特别注意的是:
1,<Fragment/>中的 android : name属性指定了在layout中实例化的是哪个Fragment类
2,<Fragment/>中必须要有个 id 属性 或者 tag属性,否则会报错
下面演示下静态加载Fragment
静态加载Fragment的步骤是:
1,准备好一个Fragment类,并且准备好相应的布局文件
2,在Activity的layout文件中,将<Fragment/>标签添加进去
3,就可以直接在Activity中获取到fragment中的控件对象了
1,准备好一个Fragment类,并且准备好相应的布局文件
---- 准备好Fragment相应的布局文件 layout_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@android:color/black"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是个Fragment"
android:textColor="@android:color/white"
android:layout_centerInParent="true"
android:textSize="20sp"
android:id="@+id/tv_fragment"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="点击按钮"
android:id="@+id/bt_fragment"/>
</RelativeLayout>
</RelativeLayout>
---- 准备好Fragment类,就是继承Fragment类,实现onCreateView(..)函数
在这里就可以直接获取Fragment中的控件对象,并修改
public class MyFragment extends Fragment {
/*
* Fragment 第一次绘制它的用户界面的时候,系统会调用此方法,为了绘制Fragment的UI,此方法必须返回一个View,
* 如果不显示UI,返回 null 即可
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_fragment, null);
TextView tv_fragment = (TextView) view.findViewById(R.id.tv_fragment);
//在这里获取fragment中的内容
tv_fragment.setText("fragment");
return view;
}
}
2,在Activity的layout文件中,将<Fragment/>标签添加进去
---- Activity的layout文件 activity_main.xml
name表示的是哪一个Fragment ,标签中必须要有id 或者 tag属性,否则会报错
<RelativeLayout 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="com.xulunpeng.down_01.MainActivity">
<!-- 注意Fragment上必须有id或者tag属性,否则会报错 -->
<fragment
android:layout_width="300dp"
android:layout_height="300dp"
android:name="com.xulunpeng.down_01.MyFragment"
android:id="@+id/fragment"/>
</RelativeLayout>
3,就可以直接在Activity中获取到fragment中的控件对象了
---- 因为fragment就在当前的布局下,fragment就已经被加载到当前的控件树下了,所以可以直接通过 findViewById 来获得fragment上的控件对象
public class MainActivity extends Activity {
TextView tv_fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 因为fragment就在当前的布局下,fragment已经被加载到当前的控件树下了,所以可以直接通过
* findViewById来获得fragment上的控件对象
*/
Button bt_fragment = (Button) findViewById(R.id.bt_fragment);
tv_fragment = (TextView) findViewById(R.id.tv_fragment);
bt_fragment.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv_fragment.setText("fragment上的内容改变了");
}
});
}
}
总结:
1,Fragment 第一次绘制它的用户界面的时候,系统会调用 onCreateView( . . ) 方法,为了绘制Fragment的UI,此方法必须返回一个View,如果不显示UI,返回 null 即可
2,在Activity中,因为fragment 就在当前的布局下,fragment已经被加载到当前的控件树下了,所以可以直接通过 findViewById 来获得fragment上的控件对象
注意:
1, 注意 <fragment/> 标签上必须有 id 或者 tag 属性,否则会报错
2,<Fragment/>标签中的 android : name属性指定了在layout中实例化的是哪个Fragment类