- package com.example.student.myapplication;
- import android.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity {
- Button button1 , button2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button1 = (Button)findViewById(R.id.button1);
- button2 = (Button)findViewById(R.id.button2);
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment1 fragment1 = new Fragment1();
- FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
- fragmentTransaction.add(R.id.frame,fragment1);
- fragmentTransaction.commit();
- }
- });
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- getFragmentManager().beginTransaction().add(R.id.frame, new Fragment2()).commit();
- }
- });
- }
- }
複製代碼- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical">
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal">
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Fragment1"
- android:id="@+id/button1"
- android:textAllCaps="false"
- android:textSize="20sp"
- android:layout_weight="1" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Fragment2"
- android:id="@+id/button2"
- android:layout_gravity="center_horizontal"
- android:textAllCaps="false"
- android:textSize="20sp"
- android:layout_weight="1" />
- </LinearLayout>
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:id="@+id/frame"
- android:visibility="visible"></FrameLayout>
- </LinearLayout>
複製代碼- package com.example.student.myapplication;
- import android.os.Bundle;
- import android.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * A simple {@link Fragment} subclass.
- */
- public class Fragment1 extends Fragment {
- public Fragment1() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment1, container, false);
- }
- }
複製代碼- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.student.myapplication.Fragment1"
- android:background="#b7fbeb">
- <!-- TODO: Update blank fragment layout -->
- <TextView android:layout_width="match_parent" android:layout_height="match_parent"
- android:text="Fragment 1"
- android:textSize="28dp"
- android:gravity="center"
- android:textColor="#000000" />
- </FrameLayout>
複製代碼- package com.example.student.myapplication;
- import android.os.Bundle;
- import android.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * A simple {@link Fragment} subclass.
- */
- public class Fragment2 extends Fragment {
- public Fragment2() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment2, container, false);
- }
- }
複製代碼- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.student.myapplication.Fragment2"
- android:background="#ffc0c1">
- <!-- TODO: Update blank fragment layout -->
- <TextView android:layout_width="match_parent" android:layout_height="match_parent"
- android:text="Fragment 2"
- android:textSize="28dp"
- android:textColor="#000000"
- android:gravity="center" />
- </FrameLayout>
複製代碼 |