返回列表 發帖
  1. package com.example.shain.fragment;

  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 btn1, btn2;

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

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

  15.         btn1.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.         btn2.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. }
複製代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
  5.     android:paddingRight="@dimen/activity_horizontal_margin"
  6.     android:paddingTop="@dimen/activity_vertical_margin"
  7.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

  8.     <Button
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:text="Fragment1"
  12.         android:textSize="20sp"
  13.         android:id="@+id/button1"
  14.         android:layout_alignParentTop="true"
  15.         android:layout_alignParentLeft="true"
  16.         android:layout_alignParentStart="true" />
  17.     <Button
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"
  20.         android:text="Fragment2"
  21.         android:textSize="20sp"
  22.         android:layout_alignParentRight="true"
  23.         android:layout_alignParentEnd="true"
  24.         android:id="@+id/button2" />

  25.     <FrameLayout
  26.         android:layout_width="match_parent"
  27.         android:layout_height="match_parent"
  28.         android:layout_alignRight="@+id/button2"
  29.         android:layout_alignEnd="@+id/button2"
  30.         android:layout_below="@+id/button1"
  31.         android:id="@+id/frame"></FrameLayout>

  32. </RelativeLayout>
  33. [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>
  1. [/code]<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" tools:context="com.example.shain.fragment.Fragment2"
  4.     android:id="@+id/fragment2"
  5.     android:background="#e3babc">

  6.     <!-- TODO: Update blank fragment layout -->

  7. </FrameLayout>
複製代碼

TOP

返回列表