編輯:初階開發
這段時間都在看 Java,Android 放了一陣子,現在回過頭慢慢補。這次要做的是一個圖片加兩行文字的多欄清單。
要達到這個效果,最省事的做法是 ListActivity 搭配 SimpleAdapter。
每一列視為 ArrayList 的一個元素,而每一列內部的三個欄位則用 HashMap 存放:
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.checked);
map.put("ItemTitle", "Level" + i);
map.put("ItemText", "this is under Level" + i);
list.add(map);
每一列中那三個元件(一個 ImageView、兩個 TextView)的排版,需要另外寫一個 XML 版面檔。
public class SimpleAdapterDemo extends ListActivity {
// 填充 list 資料
private ArrayList<HashMap<String, Object>> fillList() {
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 10; ++i) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.checked);
map.put("ItemTitle", "Level" + i);
map.put("ItemText", "this is under Level" + i);
list.add(map);
}
return list;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, Object>> list = fillList();
// 建立适配器,把 ArrayList 的資料對應到畫面
SimpleAdapter simAdapter = new SimpleAdapter(this, list,
R.layout.items, // 每一列的版面檔
new String[]{"ItemImage", "ItemTitle", "ItemText"}, // 三個資料的索引鍵
new int[]{R.id.ItemImage, R.id.ItemTitle, R.id.ItemText}); // 對應的 View id
setListAdapter(simAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
setTitle("點擊第" + position + "個項目");
}
}
SimpleAdapter 的建構參數順序是:Context、資料清單、每列版面、資料鍵陣列、View id 陣列。兩組陣列的順序必須一一對應,這是最容易出錯的地方。
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<ImageView
android:id="@+id/ItemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="12dip"
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/ItemTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="TextView01" />
<TextView
android:id="@+id/ItemText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ItemTitle"
android:text="TextView02" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- 當 ListActivity 沒有資料時顯示這段 -->
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="sorry, no data" />
</LinearLayout>
使用 ListActivity 時,清單的 id 必須是 @id/android:list,空資料提示的 TextView 則必須是 @id/android:empty,兩者都是系統預設的約定,寫錯就會出錯。
另外,SimpleAdapter 適合結構固定的簡單清單。若每一列需要複雜的邏輯、非同步載圖或差異化排版,就該改用 BaseAdapter 自行實作 getView,並善用 ViewHolder 模式減少 findViewById 呼叫。
用 USB 讓 Android 手機共享電腦網路
除了手機透過 USB 分享網路給電腦,也可以反向讓手機使用電腦網路。這類做法通常需要電腦端驅動、支援 USB 網路共享的 ROM,以及手機端具備足夠權限來設定網卡與路由。
Android APK 軟體漢化教學(二):從 arsc 到 classes.dex
本文承接上一篇,說明 APK 漢化的完整流程,分成四個部分:resources.arsc、XML 檔、classes.dex,以及最後的簽名。全程使用 androidRe
用 Android 手機遠端控制電腦
想在被窩裡使用電腦,卻不想離開溫暖的床?透過遠端桌面,Android 手機也能成為控制電腦的終端裝置。只要電腦與手機連上同一網路,就能在手機上操作 Windows 桌面。
Android 快捷方式(二):讓自己的 Activity 出現在 Launcher 選單裡
在桌面空白處長按,會跳出「加到主畫面」的對話框,接著選擇「捷徑」,系統便会列出所有可建立捷徑的 Activity——每個項目顯示對應應用的圖示與名稱。 問題是:怎麼讓自