Android Studio 中的 Fragment 教程及示例


此示例演示了 Android Studio 中的 Fragment 教程及示例

步驟 1 − 在 Android Studio 中建立一個新專案,轉到 檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <Button
      android:id = "@+id/fragment1"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_alignParentTop = "true"
      android:layout_centerHorizontal = "true"
      android:layout_marginTop = "27dp"
      android:text = "fragment1"/>
   <Button
      android:id = "@+id/fragment2"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_alignParentTop = "true"
      android:layout_centerHorizontal = "true"
      android:layout_marginTop = "27dp"
      android:text = "fragment2"/>
   <LinearLayout
      android:id = "@+id/layout"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:orientation = "vertical">
   </LinearLayout>
</LinearLayout>

在上面的程式碼中,我們使用了按鈕檢視和線性佈局來顯示不同的片段。

步驟 3 − 將以下程式碼新增到 src /MainActivity.java

package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {
   @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final android.support.v4.app.Fragment first = new FirstFragment();
      final android.support.v4.app.Fragment second = new SecondFragment();
      findViewById(R.id.fragment1).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.layout, first);
            fragmentTransaction.commit();
         }
      });
      findViewById(R.id.fragment2).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.layout, second);
            fragmentTransaction.commit();
         }
      });
   }
}

步驟 4 − 將以下程式碼新增到 src / FirstFragment.java

package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class FirstFragment extends Fragment {
   TextView textView;
   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment, container, false);
      textView = view.findViewById(R.id.text);
      textView.setText("first");
      return view;
   }
}

步驟 5 − 將以下程式碼新增到 src / SecondFragment.java

package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SecondFragment extends Fragment {
   TextView textView;
   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment, container, false);
      textView = view.findViewById(R.id.text);
      textView.setText("Second");
      return view;
   }
}

步驟 6 − 將以下程式碼新增到 res/layout/fragment.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout
   xmlns:android = "http://schemas.android.com/apk/res/android"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "match_parent">
   <TextView
      android:id = "@+id/text"
      android:textSize = "30sp"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent" />
</LinearLayout>

讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要在 Android Studio 中執行應用程式,請開啟您專案中的一個活動檔案,然後單擊執行  工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 –

現在單擊按鈕,將顯示如下所示的結果 –


點選 這裡 下載專案程式碼

更新於: 2019年7月30日

5K+ 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告