- Android 基礎
- Android - 首頁
- Android - 概覽
- Android - 環境設定
- Android - 架構
- Android - 應用元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動
- Android - 服務
- Android - 廣播接收器
- Android - 內容提供程式
- Android - 碎片
- Android - 意圖/過濾器
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送電子郵件
- Android - 傳送簡訊
- Android - 電話呼叫
- 釋出 Android 應用
- Android 有用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕捉
- Android - 音訊管理器
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 影像效果
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入微調器
- Android - 本地化
- Android - 登入螢幕
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 有用資源
- Android - 問答
- Android - 有用資源
- Android - 討論
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.java和LM_Fragement.java,以定義您的碎片和關聯方法。 |
| 4 | 建立佈局檔案res/layout/lm_fragment.xml和res/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.java和PM_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.xml和pm_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 上並啟動它,如果您的設定和應用程式一切正常,它將顯示模擬器視窗,您可以在其中單擊“選單”按鈕以檢視以下視窗。請耐心等待,因為這可能需要一些時間,具體取決於您的計算機速度 -
要更改模擬器螢幕的模式,讓我們執行以下操作 -
在 Mac 上按fn+control+F11將橫向更改為縱向,反之亦然。
在 Windows 上按ctrl+F11。
在 Linux 上按ctrl+F11。
更改模式後,您將能夠看到為橫向模式實現的 GUI,如下所示 -
這樣,您可以使用相同的活動,但透過不同的碎片使用不同的 GUI。您可以根據需要為不同的 GUI 使用不同型別的 GUI 元件。
