返回列表 發帖
Main:
  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 f1 = new Fragment1();
  19.                 FragmentTransaction ft = getFragmentManager().beginTransaction();
  20.                 ft.add(R.id.frame, f1);
  21.                 ft.commit();
  22.             }
  23.         });
  24.         button2.setOnClickListener(new View.OnClickListener() {
  25.             @Override
  26.             public void onClick(View v) {
  27.                 Fragment2 f2 = new Fragment2();
  28.                 FragmentTransaction ft = getFragmentManager().beginTransaction();
  29.                 ft.add(R.id.frame, f2);
  30.                 ft.commit();
  31.             }
  32.         });
  33.     }
  34. }
複製代碼
Fragment1:
  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. public class Fragment1 extends Fragment {
  8.     public Fragment1() {
  9.     }
  10.     @Override
  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  12.                              Bundle savedInstanceState) {
  13.         // Inflate the layout for this fragment
  14.         return inflater.inflate(R.layout.fragment1, container, false);
  15.     }
  16. }
複製代碼
Fragment2:
  1. package com.example.student.myapplication;

  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.     public Fragment2(){

  9.     }
  10.     @Override
  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  12.         return inflater.inflate(R.layout.fragment2,container,false);
  13.     }
  14. }
複製代碼
Fragment1_layout:
  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.     <TextView
  7.         android:layout_width="match_parent"
  8.         android:layout_height="match_parent"
  9.         android:text="Fragment1"
  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:background="#e12a45f"
  16.         android:visibility="visible" />
  17. </LinearLayout>
複製代碼
fragment2_layout:
  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="#ffc7c7">

  6.     <TextView
  7.         android:layout_width="match_parent"
  8.         android:layout_height="match_parent"
  9.         android:text="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:background="#edc7f5"
  16.         android:visibility="visible" />
  17. </LinearLayout>
複製代碼
main_layout:
  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="185dp"
  14.             android:layout_height="59dp"
  15.             android:text="Fragment1"
  16.             android:id="@+id/button1"
  17.             android:textAllCaps="false"
  18.             android:textSize="20sp" />

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

  28.     <FrameLayout
  29.         android:layout_width="fill_parent"
  30.         android:layout_height="0dp"
  31.         android:layout_weight="0.95"
  32.         android:id="@+id/frame"
  33.         android:visibility="visible"></FrameLayout>
  34. </LinearLayout>
複製代碼

TOP

返回列表