本帖最後由 tonyh 於 2018-1-13 13:34 編輯
layout\activity_main.xml- <?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">
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:id="@+id/frame1"
- android:visibility="visible"
- android:orientation="horizontal"></FrameLayout>
- </LinearLayout>
複製代碼 layout-land\activity_main.xml- <?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:baselineAligned="false">
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_gravity="center_vertical"
- android:layout_weight="2"
- android:id="@+id/frame1"></FrameLayout>
- <FrameLayout
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_gravity="center_vertical"
- android:layout_weight="3"
- android:id="@+id/frame2"></FrameLayout>
- </LinearLayout>
複製代碼 layout\fragment1.xml- <?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">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Go to Fragment2"
- android:id="@+id/button1"
- android:layout_gravity="center_horizontal"
- android:textAllCaps="false"
- android:textSize="24sp" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Go to Fragment3"
- android:id="@+id/button2"
- android:layout_gravity="center_horizontal"
- android:textAllCaps="false"
- android:textSize="24sp" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="This is Fragment1"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal"
- android:gravity="center"
- android:textSize="28sp"
- android:textColor="@android:color/black"
- android:visibility="visible" />
- </LinearLayout>
複製代碼 layout\fragment2.xml- <?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="#fed4f4">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="This is Fragment2"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal"
- android:gravity="center"
- android:textSize="28sp"
- android:textColor="@android:color/black"
- android:visibility="visible" />
- </LinearLayout>
複製代碼 layout\fragment3.xml- <?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="#daffce">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="This is Fragment3"
- android:id="@+id/textView"
- android:layout_gravity="center_horizontal"
- android:gravity="center"
- android:textSize="28sp"
- android:textColor="@android:color/black"
- android:visibility="visible" />
- </LinearLayout>
複製代碼 MainActivity.java- package org.istak.ch38_fragment;
- import android.app.Activity;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Fragment1 f1 = new Fragment1();
- //FragmentManager fm = getSupportFragmentManager();
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.add(R.id.frame1, f1, null);
- ft.commit();
- }
- }
複製代碼 Fragment1.java- package org.istak.ch38_fragment;
- import android.app.Fragment;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- /**
- * Created by Tony on 2017/11/14.
- */
- public class Fragment1 extends Fragment{
- Button btn1,btn2;
- MainActivity activity;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- activity= (MainActivity) getActivity();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- getFragmentManager().popBackStack(); //彈出最上層的堆棧
- View v=inflater.inflate(R.layout.fragment1,container,false);
- btn1= (Button) v.findViewById(R.id.button1);
- btn2= (Button) v.findViewById(R.id.button2);
- btn1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment2 f2 = new Fragment2();
- //FragmentManager fm=getFragmentManager();
- FragmentTransaction ft =getFragmentManager().beginTransaction();
- if(activity.findViewById(R.id.frame2)!=null) //若 frame2 存在,則代表此時裝置是橫向擺放的 (橫擺時會自動抓資料夾 layout-land 中的布局)
- {
- ft.add(R.id.frame2, f2, null); //將 f2 加到 frame2 上 (畫面的右側)
- }else{
- ft.add(R.id.frame1, f2); //將 f2 加到 frame1 上 (畫面的左側)
- ft.addToBackStack(null); //加入堆疊區,使按 "返回鍵" 能回到下層的堆棧,而不會直接跳出程式
- }
- ft.commit();
- }
- });
- btn2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment3 f3 = new Fragment3();
- FragmentTransaction ft =getFragmentManager().beginTransaction();
- if(activity.findViewById(R.id.frame2)!=null)
- {
- ft.add(R.id.frame2, f3, null);
- }else{
- ft.add(R.id.frame1, f3);
- ft.addToBackStack(null);
- }
- ft.commit();
- }
- });
- return v;
- }
- }
複製代碼 Fragment2.java- package org.istak.ch38_fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * Created by Tony on 2017/11/14.
- */
- public class Fragment2 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View v=inflater.inflate(R.layout.fragment2,container,false);
- return v;
- }
- }
複製代碼 Fragment3.java- package org.istak.ch38_fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * Created by Tony on 2017/11/14.
- */
- public class Fragment3 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View v=inflater.inflate(R.layout.fragment3,container,false);
- return v;
- }
- }
複製代碼 |