Android中的Fragment使用详解之页面加载
静态加载
1、Fragment布局
<?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:orientation='vertical' ><TextViewandroid:id='@+id/textView1'android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='TextView' /><Buttonandroid:id='@+id/button1'android:layout_width='wrap_content'android:layout_height='wrap_content'android:text='Button' /></LinearLayout>
2、编写我的MyFragment
package com.cx.testdemo;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 {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// layout文件转化为View对象(需要加载的布局文件,加载layout的父ViewGroup,是否返回父窗体ViewGroup)View view = inflater.inflate(R.layout.fragment, container, false);TextView textView = (TextView) view.findViewById(R.id.textView1);textView.setText('静态加载');return view;}}
注意:在引入jar包时要引入import android.app.Fragment;,不能引入import android.support.v4.app.Fragment;。引入后者时,代码不会报错,但运行时报错。
3、在Activity的布局中使用MyFragment
<?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:orientation='vertical' ><fragmentandroid:id='@+id/fragment'android:layout_width='wrap_content'android:layout_height='wrap_content'android:name='com.cx.testdemo.MyFragment'/></LinearLayout>
注意:一定要加id属性,虽然在其他地方用不到。
4、在Activity中使用
package com.cx.testdemo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity2 extends Activity {private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main2);//直接使用fragment布局文件中的Button。静态加载特点,在Activity中对Fragment中的控件进行改变。Button button = (Button) findViewById(R.id.button1);textView = (TextView) findViewById(R.id.textView1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtextView.setText('改变');}});}}
动态加载
动态加载的前两步与静态加载相同,这里直接从第三步开始3、Activity的布局文件中将上面3中布局的fragment换成LinearLayout<?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:orientation='vertical' ><LinearLayoutandroid:id='@+id/frame'android:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='vertical' ></LinearLayout></LinearLayout>
4、在Activity中动态加载fragment
package com.cx.testdemo;/** 动态加载*/import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;public class MainActivity2 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main2);MyFragment2 fragment2 = new MyFragment2();//获取Fragment管理者FragmentManager fragmentManager = getFragmentManager();//通过Fragment管理者开启一个事务的对象FragmentTransaction beginTransaction = fragmentManager.beginTransaction();//(添加到的布局id,Fragment)beginTransaction.add(R.id.frame, fragment2);//返回键回退效果beginTransaction.addToBackStack(null);beginTransaction.commit();}}
赞 (0)
