FragMent 初级学习

FragMent 可以用在一个Activity中也可以用在多个Activity中

我们在activity中加载FragMent的时候,首先添加一个myFragMent的类

通过onCreateView方法,加载 FragMent的布局文件

package com.example.flagmentdemo;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; @SuppressLint("NewApi")
public class MyfragMent extends Fragment{ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
//return super.onCreateView(inflater, container, savedInstanceState);
View view= inflater.inflate(R.layout.flagment, container, false);
TextView text=(TextView) view.findViewById(R.id.textview);
text.setText("我是静态加载的");
return view;
} }

然后在新建的activity中加载fragment

在新的activity的布局文件main2中加一个fragment

  
   <fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment"
android:name="com.example.flagmentdemo.MyfragMent"
/>

然后再activity中 setContentView(R.layout.main2)

上一篇:@NotNull, @NotEmpty和@NotBlank之间的区别是什么?


下一篇:c – doxygen – 如何多行文件文档枚举?