編輯:關於 Android 程式設計
碎片(Fragment)
碎片可以看作另一種形式的活動,我們可以建立碎片來容納檢視。碎片總是嵌入在活動之中,一般有兩種常見形式:
1、碎片 A 與碎片 B 分別處於不同的活動中,當選擇碎片 A 中的某一項時,觸發碎片 B 啟動;
2、碎片 A 與碎片 B 處於同一個活動中,共享同一活動,以創造更佳的用戶體驗。
點此下載完整源碼(程式碼適用於本文所講內容)。
下面跟著步驟動手實作:
1、建立一個名為「Fragments」的專案,在 res/layout 資料夾下分別新建 fragment1.xml、fragment2.xml;在當前套件名稱下分別新建 Fragment1.java、Fragment2.java。
fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>fragment2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFE00"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #2"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>Fragment1.java:
package net.zenail.fragments;
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 {
// 繪製碎片 UI:使用 LayoutInflauter 物件來增大指定 XML 檔案中的 UI。
// container 參數引用父 ViewGroup,準備用於嵌入碎片的活動。
// savedInstanceState 參數允許將碎片還原到前一次保存的狀態。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}Fragment2.java:
package net.zenail.fragments;
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);
}
}2、在 main.xml 檔案中加入兩個碎片:
<fragment
android:id="@+id/fragment1"
android:name="net.zenail.fragments.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment1" />
<fragment
android:id="@+id/fragment2"
android:name="net.zenail.fragments.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment2" />3、運行,效果如下:

Android 仿支付風格的密碼鍵盤與輸入框
支付風格密碼鍵盤 許多支付類應用會使用自訂密碼鍵盤與六格輸入框,讓使用者輸入數字密碼時更直覺。這種設計通常由鍵盤區與密碼顯示區組成,並搭配刪除、完成與輸入完成回呼。 鍵
Android 桌面懸浮窗與蒙板教學:WindowManager 完整實作
現在很多安全類 App,像是 360 手機助手、百度手機助手等,都會在桌面上放一個懸浮窗,讓使用者隨時能叫出常用功能。這篇文章就來拆解這個效果的做法。先看成品:整個懸浮窗
Android ScrollView:如何偵測已經滑到底部
做「滑到底自動載入更多」的時候,第一個要解決的問題是:怎麼知道使用者已經滑到底了?做法是透過 OnTouchListener 攔截 ACTION_UP 事件,再比對 S
Android 自定義軟鍵盤實作:打造專屬的密碼輸入鍵盤
下面示範如何在 Android 中實作一個自定義的軟鍵盤,特別針對密碼輸入場景做了一些客製化處理,例如按鍵隨機排列、輸入內容短暫顯示後變為星號等。 圖一為搜狗輸入法