Main:- 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 f1 = new Fragment1();
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.add(R.id.frame, f1);
- ft.commit();
- }
- });
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment2 f2 = new Fragment2();
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.add(R.id.frame, f2);
- ft.commit();
- }
- });
- }
- }
複製代碼 Fragment1:- 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;
- public class Fragment1 extends Fragment {
- public Fragment1() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment1, container, false);
- }
- }
複製代碼 Fragment2:- package com.example.student.myapplication;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class Fragment2 extends Fragment {
- public Fragment2(){
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment2,container,false);
- }
- }
複製代碼 Fragment1_layout:- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#c0e6ff">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="Fragment1"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal"
- android:gravity="center"
- android:textSize="28sp"
- android:textColor="@android:color/black"
- android:background="#e12a45f"
- android:visibility="visible" />
- </LinearLayout>
複製代碼 fragment2_layout:- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ffc7c7">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="Fragment2"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal"
- android:gravity="center"
- android:textSize="28sp"
- android:textColor="@android:color/black"
- android:background="#edc7f5"
- android:visibility="visible" />
- </LinearLayout>
複製代碼 main_layout:- <?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="185dp"
- android:layout_height="59dp"
- android:text="Fragment1"
- android:id="@+id/button1"
- android:textAllCaps="false"
- android:textSize="20sp" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:text="Fragment2"
- android:id="@+id/button2"
- android:layout_gravity="center_horizontal"
- android:textAllCaps="false"
- android:textSize="20sp" />
- </LinearLayout>
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="0.95"
- android:id="@+id/frame"
- android:visibility="visible"></FrameLayout>
- </LinearLayout>
複製代碼 |