• Android Video Tutorials

Android - 單一碎片



單幀碎片

單幀碎片專為小型螢幕裝置(如手持裝置(手機))設計,並且應高於 Android 3.0 版本。

示例

此示例將向您解釋如何建立自己的碎片。在這裡,我們將建立兩個碎片,其中一個將在裝置處於橫向模式時使用,另一個碎片將在縱向模式下使用。因此,讓我們按照以下步驟操作,類似於我們在建立Hello World 示例時所遵循的步驟 -

步驟 描述
1 您將使用 Android Studio IDE 建立一個 Android 應用程式,並將其命名為MyFragments,位於包com.example.myfragments下,並使用空白 Activity。
2 修改主活動檔案MainActivity.java,如下面的程式碼所示。在這裡,我們將檢查裝置的方向,並相應地在不同的碎片之間切換。
3 在包com.example.myfragments下建立兩個 Java 檔案PM_Fragment.javaLM_Fragement.java,以定義您的碎片和關聯方法。
4 建立佈局檔案res/layout/lm_fragment.xmlres/layout/pm_fragment.xml,併為這兩個碎片定義您的佈局。
5 修改res/layout/activity_main.xml檔案的預設內容以包含這兩個碎片。
6 res/values/strings.xml檔案中定義所需的常量
7 執行應用程式以啟動 Android 模擬器並驗證對應用程式所做的更改的結果。

以下是修改後的主活動檔案MainActivity.java的內容 -

package com.example.myfragments;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;

public class MainActivity extends Activity {

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Configuration config = getResources().getConfiguration();

      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

      /**
         * Check the device orientation and act accordingly
      */
		
      if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         /**
            * Landscape mode of the device
         */
         LM_Fragement ls_fragment = new LM_Fragement();
         fragmentTransaction.replace(android.R.id.content, ls_fragment);
      }else{
         /**
            * Portrait mode of the device
         */
         PM_Fragement pm_fragment = new PM_Fragement();
         fragmentTransaction.replace(android.R.id.content, pm_fragment);
      }
      fragmentTransaction.commit();
   }

}

建立兩個碎片檔案LM_Fragement.javaPM_Fragment.java

以下是LM_Fragement.java檔案的內容 -

package com.example.myfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
   * Created by TutorialsPoint7 on 8/23/2016.
*/

public class LM_Fragement extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      /**
         * Inflate the layout for this fragment
      */
      return inflater.inflate(R.layout.lm_fragment, container, false);
   }
}

以下是PM_Fragement.java檔案的內容 -

package com.example.myfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
   * Created by TutorialsPoint7 on 8/23/2016.
*/

public class PM_Fragement extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      /**
         * Inflate the layout for this fragment
      */
      return inflater.inflate(R.layout.pm_fragment, container, false);
   }
}

res/layout目錄下建立兩個佈局檔案lm_fragement.xmlpm_fragment.xml

以下是lm_fragement.xml檔案的內容 -

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#7bae16">
   
   <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/landscape_message"
      android:textColor="#000000"
      android:textSize="20px" />

<!-- More GUI components go here  -->

</LinearLayout>

以下是pm_fragment.xml檔案的內容 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#666666">
   
   <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/portrait_message"
      android:textColor="#000000"
      android:textSize="20px" />

<!-- More GUI components go here  -->

</LinearLayout>

以下是包含碎片的res/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="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal">

   <fragment
      android:name="com.example.fragments"
      android:id="@+id/lm_fragment"
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="match_parent" />
   
   <fragment
      android:name="com.example.fragments"
      android:id="@+id/pm_fragment"
      android:layout_weight="2"
      android:layout_width="0dp"
      android:layout_height="match_parent" />

</LinearLayout>

確保您具有res/values/strings.xml檔案的以下內容 -

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">My Application</string>
   <string name="landscape_message">This is Landscape mode fragment</string>
   <string name="portrait_message">This is Portrait mode fragment></string>
</resources>

讓我們嘗試執行我們剛剛建立的修改後的MyFragments應用程式。我假設您在進行環境設定時已建立了AVD。要從 Android Studio 執行該應用程式,請開啟專案中的一個活動檔案,然後單擊工具欄中的“執行”圖示。Android Studio 將應用程式安裝到您的 AVD 上並啟動它,如果您的設定和應用程式一切正常,它將顯示模擬器視窗,您可以在其中單擊“選單”按鈕以檢視以下視窗。請耐心等待,因為這可能需要一些時間,具體取決於您的計算機速度 -

Android Portrait Fragment Demo

要更改模擬器螢幕的模式,讓我們執行以下操作 -

  • 在 Mac 上按fn+control+F11將橫向更改為縱向,反之亦然。

  • 在 Windows 上按ctrl+F11

  • 在 Linux 上按ctrl+F11

更改模式後,您將能夠看到為橫向模式實現的 GUI,如下所示 -

Android Landscape Fragment Demo

這樣,您可以使用相同的活動,但透過不同的碎片使用不同的 GUI。您可以根據需要為不同的 GUI 使用不同型別的 GUI 元件。

android_fragments.htm
廣告

© . All rights reserved.