- package com.example.shain.fragment;
- 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 btn1, btn2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn1 = (Button) findViewById(R.id.button1);
- btn2 = (Button) findViewById(R.id.button2);
- btn1.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();
- }
- });
- btn2.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();
- }
- });
- }
- }
複製代碼- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Fragment1"
- android:textSize="20sp"
- android:id="@+id/button1"
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Fragment2"
- android:textSize="20sp"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"
- android:id="@+id/button2" />
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignRight="@+id/button2"
- android:layout_alignEnd="@+id/button2"
- android:layout_below="@+id/button1"
- android:id="@+id/frame"></FrameLayout>
- </RelativeLayout>
- [code]
複製代碼 <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.shain.fragment.Fragment1"
android:id="@+id/fragment1"
android:background="#afe6ee">
<!-- TODO: Update blank fragment layout -->
</FrameLayout>- [/code]<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.shain.fragment.Fragment2"
- android:id="@+id/fragment2"
- android:background="#e3babc">
- <!-- TODO: Update blank fragment layout -->
- </FrameLayout>
複製代碼 |