編輯:關於 Android 程式設計
可以把 Fragment 理解成一個「小型 Activity」——幾乎 Activity 能做的事它都能做,差別在於 Fragment 可以嵌入 Activity。一個 Activity 能裝多個 Fragment,還能在運行時依需求切換,這正是為了適配不同螢幕尺寸而生的設計。
嵌入 Fragment 有兩條路:靜態用 XML 宣告,動態則在 Java 程式碼中完成。
package bill.su.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
對應的版面 fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fragment_one"
android:textSize="25sp"
android:textColor="#FF0000" />
</LinearLayout>
package bill.su.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
fragment2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFE00" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fragment_two"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bill.su.fragment.MainActivity"
android:orientation="horizontal" >
<fragment
android:name="bill.su.fragment.Fragment1"
android:id="@+id/fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:name="bill.su.fragment.Fragment2"
android:id="@+id/fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
用 layout_weight 平均分配寬度,就能得到左右並排的兩欄效果:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragManager = getFragmentManager();
FragmentTransaction fragTrans = fragManager.beginTransaction();
// 取得螢幕顯示資訊,判斷橫屏或豎屏
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
if (d.getWidth() < d.getHeight()) {
Fragment1 frag1 = new Fragment1();
fragTrans.replace(android.R.id.content, frag1);
} else {
Fragment2 frag2 = new Fragment2();
fragTrans.replace(android.R.id.content, frag2);
}
fragTrans.commit();
}
動態載入靠的是 FragmentManager 與 FragmentTransaction 這兩個類別搭配使用。
範例中的 WindowManager 用來取得螢幕長寬,判斷目前是橫屏還是豎屏,再據此載入不同的 Fragment,呈現不同版面。
最後那句 fragTrans.commit() 不能省——少了它,Fragment 不會真正被載入。另外要注意 commit 是非同步的,如果你需要在提交後立刻操作 Fragment,應該改用 commitNow() 或透過 executePendingTransactions 處理。
手把手解析 Resources.arsc 的二進位結構
一、前言 APK 裡的 Resources.arsc 是什麼,多數 Android 開發者都聽過——它就是 App 的資源索引表。本文會結合實例把它的格式拆開來看,讀完之
透過 JNI 呼叫美圖秀秀 so 實作 Android 圖片美化
實作概念 這篇文章說明如何透過 JNI 載入 libmtimage-jni.so,並在 Java 端呼叫原生影像處理方法。使用前要確認 so 與裝置 ABI 相符,並把檔
仿知乎日報實現:MainFragment的十三個頁面與數據加載
頁面結構設計在仿知乎日報項目中,MainFragment內部使用ViewPager承載多個主題頁面。頁面包括首頁、日常心理學、用戶推薦日報、電影日報、不許無聊、設計日報、
Android 四大組件工作過程探索(一):Activity 啟動全流程
四大組件的運行狀態 Android 四大組件中,除 BroadcastReceiver 外,其餘三種(Activity、Service、ContentProvider)都