返回列表 發帖
本帖最後由 黃璽安 於 2019-9-1 13:51 編輯
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity"
  6.     android:orientation="vertical">

  7.     <FrameLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="0dp"
  10.         android:layout_weight="1"
  11.         android:id="@+id/frame1"
  12.         android:visibility="visible"
  13.         android:orientation="horizontal"></FrameLayout>
  14. </LinearLayout>
複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity"
  6.     android:baselineAligned="false">

  7.     <FrameLayout
  8.         android:layout_width="0dp"
  9.         android:layout_height="match_parent"
  10.         android:layout_gravity="center_vertical"
  11.         android:layout_weight="2"
  12.         android:id="@+id/frame1"></FrameLayout>

  13.     <FrameLayout
  14.         android:layout_width="0dp"
  15.         android:layout_height="match_parent"
  16.         android:layout_gravity="center_vertical"
  17.         android:layout_weight="3"
  18.         android:id="@+id/frame2"></FrameLayout>
  19. </LinearLayout>
複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#c0e6ff">

  6.     <Button
  7.         android:layout_width="match_parent"
  8.         android:layout_height="wrap_content"
  9.         android:text="Go to Fragment2"
  10.         android:id="@+id/button1"
  11.         android:layout_gravity="center_horizontal"
  12.         android:textAllCaps="false"
  13.         android:textSize="24sp" />
  14.     <Button
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content"
  17.         android:text="Go to Fragment3"
  18.         android:id="@+id/button2"
  19.         android:layout_gravity="center_horizontal"
  20.         android:textAllCaps="false"
  21.         android:textSize="24sp" />

  22.     <TextView
  23.         android:layout_width="match_parent"
  24.         android:layout_height="match_parent"
  25.         android:text="This is Fragment1"
  26.         android:id="@+id/textView"
  27.         android:layout_gravity="center_horizontal"
  28.         android:gravity="center"
  29.         android:textSize="28sp"
  30.         android:textColor="@android:color/black"
  31.         android:visibility="visible" />

  32. </LinearLayout>
複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#fed4f4">

  6.     <TextView
  7.         android:layout_width="match_parent"
  8.         android:layout_height="match_parent"
  9.         android:text="This is Fragment2"
  10.         android:id="@+id/textView"
  11.         android:layout_gravity="center_horizontal"
  12.         android:gravity="center"
  13.         android:textSize="28sp"
  14.         android:textColor="@android:color/black"
  15.         android:visibility="visible" />
  16. </LinearLayout>
複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#daffce">

  6.     <TextView
  7.         android:layout_width="match_parent"
  8.         android:layout_height="match_parent"
  9.         android:text="This is Fragment3"
  10.         android:id="@+id/textView"
  11.         android:layout_gravity="center_horizontal"
  12.         android:gravity="center"
  13.         android:textSize="28sp"
  14.         android:textColor="@android:color/black"
  15.         android:visibility="visible" />
  16. </LinearLayout>
複製代碼
  1. package com.example.shain.fragment2;

  2.         import android.app.Activity;
  3.         import android.app.FragmentTransaction;
  4.         import android.os.Bundle;

  5. public class MainActivity extends Activity {

  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_main);
  10.         Fragment1 f1 = new Fragment1();
  11.         //FragmentManager fm = getSupportFragmentManager();
  12.         FragmentTransaction ft = getFragmentManager().beginTransaction();
  13.         ft.add(R.id.frame1, f1, null);
  14.         ft.commit();
  15.     }
  16. }
複製代碼
  1. package com.example.shain.fragment2;

  2. import android.app.Fragment;
  3. import android.app.FragmentTransaction;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;

  9. public class Fragment1 extends Fragment {

  10.     Button btn1, btn2;
  11.     MainActivity activity;

  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         activity = (MainActivity) getActivity();
  16.     }

  17.     @Override
  18.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  19.         getFragmentManager().popBackStack();

  20.         View v = inflater.inflate(R.layout.fragment1, container, false);

  21.         btn1 = (Button) v.findViewById(R.id.button1);
  22.         btn2 = (Button) v.findViewById(R.id.button2);

  23.         btn1.setOnClickListener(new View.OnClickListener() {
  24.             @Override
  25.             public void onClick(View v) {
  26.                 Fragment2 f2 = new Fragment2();
  27.                 //FragmentManager fm=getFragmentManager();
  28.                 FragmentTransaction ft = getFragmentManager().beginTransaction();
  29.                 if (activity.findViewById(R.id.frame2) != null)
  30.                 {
  31.                     ft.add(R.id.frame2, f2, null);
  32.                 } else {
  33.                     ft.add(R.id.frame1, f2);
  34.                     ft.addToBackStack(null);      
  35.                 }
  36.                 ft.commit();
  37.             }
  38.         });

  39.         btn2.setOnClickListener(new View.OnClickListener() {
  40.             @Override
  41.             public void onClick(View v) {
  42.                 Fragment3 f3 = new Fragment3();
  43.                 FragmentTransaction ft = getFragmentManager().beginTransaction();
  44.                 if (activity.findViewById(R.id.frame2) != null) {
  45.                     ft.add(R.id.frame2, f3, null);
  46.                 } else {
  47.                     ft.add(R.id.frame1, f3);
  48.                     ft.addToBackStack(null);
  49.                 }
  50.                 ft.commit();
  51.             }
  52.         });
  53.         return v;
  54.     }
  55. }
複製代碼
  1. package com.example.shain.fragment2;


  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;

  7. public class Fragment2 extends Fragment {

  8.     @Override
  9.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  10.         View v = inflater.inflate(R.layout.fragment2, container, false);
  11.         return v;
  12.     }
  13. }
複製代碼
  1. package com.example.shain.fragment2;


  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;

  7. public class Fragment3 extends Fragment {

  8.     @Override
  9.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  10.         View v = inflater.inflate(R.layout.fragment3, container, false);
  11.         return v;
  12.     }
  13. }
複製代碼

TOP

返回列表