碎片1
视图
<?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="#00FF00" > <TextView android:id="@+id/lblFragment1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #1" android:textColor="#000000" android:textSize="25sp"/> </LinearLayout>
代码
package com.example.androidtest; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("Fragment1", "onCreateView"); return inflater.inflate(R.layout.fragment1, container, false); } @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.d("Fragment1", "onAttach"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("Fragment1", "onCreate"); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.d("Fragment1", "onActivityCreated"); } @Override public void onStart() { super.onStart(); Log.d("Fragment1", "onStart"); } @Override public void onResume() { super.onResume(); Log.d("Fragment1", "onResume"); } @Override public void onPause() { super.onPause(); Log.d("Fragment1", "onPause"); } @Override public void onStop() { super.onStop(); Log.d("Fragment1", "onStop"); } @Override public void onDestroyView() { super.onDestroyView(); Log.d("Fragment1", "onDestroyView"); } @Override public void onDetach() { super.onDetach(); Log.d("Fragment1", "onDetach"); } }
碎片2
视图
<?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="#FFFE00" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #2" android:textColor="#000000" android:textSize="25sp"/> <Button android:id="@+id/btnGetText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get text in Fragment #1" android:textColor="#000000" android:onClick="onClick"/> </LinearLayout>
代码
package com.example.androidtest; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, container, false); } @Override public void onStart() { super.onStart(); //getActivity() 返回与当前碎片关联的活动 Button btnGetText = (Button) getActivity().findViewById(R.id.btnGetText); btnGetText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView lbl = (TextView) getActivity().findViewById(R.id.lblFragment1); Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT).show(); } }); } }
活动
视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" tools:context="${relativePackage}.${activityClass}" > <fragment android:name="com.example.androidtest.Fragment1" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> <fragment android:name="com.example.androidtest.Fragment2" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="0px" android:layout_height="match_parent" /> </LinearLayout>
代码
package com.example.androidtest; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Display; import android.view.WindowManager; public class FragmentsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragments); /* FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); WindowManager wm = getWindowManager(); Display d = wm.getDefaultDisplay(); if(d.getWidth() > d.getHeight()){//landscape mode Fragment1 fragment1 = new Fragment1(); //android.R.id.content活动的内容视图 fragmentTransaction.replace(android.R.id.content, fragment1); }else{//portrait mode Fragment2 fragment2 = new Fragment2(); fragmentTransaction.replace(android.R.id.content, fragment2); } //这段代码确保了当把碎片添加到活动中后,用户可以单击Back按钮移除它。 fragmentTransaction.addToBackStack(null); fragmentTransaction.commit();*/ } }
运行效果