二、Fragment的定义
public class FragMentPageA extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_a, container, false); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffff0000" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change page"/> <TextView android:id="@+id/text_a" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="this is fragment A" android:layout_gravity="center" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment class="com.example.fragment.FragMentPageA" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <LinearLayout android:id="@+id/layout_two" android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="1"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffff0000" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change page" android:textSize="30dp"/> <TextView android:id="@+id/text_a" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="this is fragment A" android:layout_gravity="center" android:textSize="30dp" /> </LinearLayout>
定义包含类FragMentPageB的填充内容的布局文件fragment_b.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ff0f0f0f"> <TextView android:id="@+id/text_b" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="this is fragment B" android:layout_gravity="center" android:textSize="30dp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ff000fff"> <TextView android:id="@+id/text_b" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="this is fragment C" android:layout_gravity="center" android:text="30dp"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment class="com.example.fragment.FragMentPageA" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <LinearLayout android:id="@+id/layout_two" android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="1"/> </LinearLayout>
类FragMentPageA的定义
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * @author memegood11 * */ public class FragMentPageA extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_a, container, false); } }类FragMentPageB的定义
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * @author memegood11 * */ public class FragMentPageB extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_b, container, false); } }
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * @author memegood11 * */ public class FragMentPageC extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_c, container, false); } }
主页面Activity的定义:
package com.example.fragment; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.View.OnClickListener; /** * @author memegood11 * */ public class MainActivity extends FragmentActivity { FragMentPageB fragmentB; FragMentPageC fragmentC; FragmentManager fragmentMgr; boolean isPageC = false; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); setContentView(R.layout.main); fragmentMgr = getSupportFragmentManager(); fragmentB = new FragMentPageB(); fragmentC = new FragMentPageC(); FragmentTransaction ft = fragmentMgr.beginTransaction(); ft.add(R.id.layout_two, fragmentB); ft.add(R.id.layout_two, fragmentC); ft.commit(); } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onStart() { super.onStart(); findViewById(R.id.btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FragmentTransaction ft = fragmentMgr.beginTransaction(); if(isPageC){ isPageC = false; ft.hide(fragmentC); ft.show(fragmentB); }else{ isPageC = true; ft.hide(fragmentB); ft.show(fragmentC); } ft.commit(); } }); } @Override protected void onStop() { super.onStop(); } }fragment就介绍到这里了,有问题的同仁可以留言,我们一起讨论。