返回列表 發帖
  1. package com.example.student.myapplication;

  2. import android.app.FragmentTransaction;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;

  7. public class MainActivity extends AppCompatActivity {

  8.     Button button1 , button2;

  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);

  13.         button1 = (Button)findViewById(R.id.button1);
  14.         button2 = (Button)findViewById(R.id.button2);

  15.         button1.setOnClickListener(new View.OnClickListener() {
  16.             @Override
  17.             public void onClick(View v) {

  18.                 Fragment1 fragment1 = new Fragment1();
  19.                 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
  20.                 fragmentTransaction.add(R.id.frame,fragment1);
  21.                 fragmentTransaction.commit();
  22.             }
  23.         });


  24.         button2.setOnClickListener(new View.OnClickListener() {
  25.             @Override
  26.             public void onClick(View v) {
  27.                 getFragmentManager().beginTransaction().add(R.id.frame, new Fragment2()).commit();
  28.             }
  29.         });
  30.     }
  31. }
複製代碼
  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.     <LinearLayout
  8.         android:orientation="horizontal"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:layout_gravity="center_horizontal">

  12.         <Button
  13.             android:layout_width="fill_parent"
  14.             android:layout_height="wrap_content"
  15.             android:text="Fragment1"
  16.             android:id="@+id/button1"
  17.             android:textAllCaps="false"
  18.             android:textSize="20sp"
  19.             android:layout_weight="1" />

  20.         <Button
  21.             android:layout_width="fill_parent"
  22.             android:layout_height="wrap_content"
  23.             android:text="Fragment2"
  24.             android:id="@+id/button2"
  25.             android:layout_gravity="center_horizontal"
  26.             android:textAllCaps="false"
  27.             android:textSize="20sp"
  28.             android:layout_weight="1" />
  29.     </LinearLayout>

  30.     <FrameLayout
  31.         android:layout_width="fill_parent"
  32.         android:layout_height="0dp"
  33.         android:layout_weight="1"
  34.         android:id="@+id/frame"
  35.         android:visibility="visible"></FrameLayout>
  36. </LinearLayout>
複製代碼
  1. package com.example.student.myapplication;


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


  7. /**
  8. * A simple {@link Fragment} subclass.
  9. */
  10. public class Fragment1 extends Fragment {


  11.     public Fragment1() {
  12.         // Required empty public constructor
  13.     }


  14.     @Override
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  16.                              Bundle savedInstanceState) {
  17.         // Inflate the layout for this fragment
  18.         return inflater.inflate(R.layout.fragment1, container, false);
  19.     }


  20. }
複製代碼
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     tools:context="com.example.student.myapplication.Fragment1"
  5.     android:background="#b7fbeb">

  6.     <!-- TODO: Update blank fragment layout -->
  7.     <TextView android:layout_width="match_parent" android:layout_height="match_parent"
  8.         android:text="Fragment 1"
  9.         android:textSize="28dp"
  10.         android:gravity="center"
  11.         android:textColor="#000000" />

  12. </FrameLayout>
複製代碼
  1. package com.example.student.myapplication;


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


  7. /**
  8. * A simple {@link Fragment} subclass.
  9. */
  10. public class Fragment2 extends Fragment {


  11.     public Fragment2() {
  12.         // Required empty public constructor
  13.     }


  14.     @Override
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  16.                              Bundle savedInstanceState) {
  17.         // Inflate the layout for this fragment
  18.         return inflater.inflate(R.layout.fragment2, container, false);
  19.     }


  20. }
複製代碼
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     tools:context="com.example.student.myapplication.Fragment2"
  5.     android:background="#ffc0c1">

  6.     <!-- TODO: Update blank fragment layout -->
  7.     <TextView android:layout_width="match_parent" android:layout_height="match_parent"
  8.         android:text="Fragment 2"
  9.         android:textSize="28dp"
  10.         android:textColor="#000000"
  11.         android:gravity="center" />

  12. </FrameLayout>
複製代碼

TOP

返回列表