編輯:Android 開發實例
上一篇談的是最基礎的 ListView,每個 Item 只有兩行文字。這次要往前推一步,讓 Item 左側多出一張圖示,變成圖文並排的樣式,同時把 RelativeLayout 的相對定位用起來。
main.xml 與上一篇完全相同,這裡不再重複貼出。真正要動的是 Item 的版面檔 my_imageitem.xml,它就是決定單列外觀的地方。
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- android:id="@+id/RelativeLayout01"
- android:layout_width="fill_parent"
- xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingLeft="12dip">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/ItemImage">
- </ImageView>
- <TextView
- android:text="TextView01"
- android:layout_height="wrap_content"
- android:textSize="30dip"
- android:layout_width="fill_parent"
- android:layout_toRightOf="@+id/ItemImage"
- android:id="@+id/ItemTitle">
- </TextView>
- <TextView
- android:text="TextView02"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_toRightOf="@+id/ItemImage"
- android:layout_below="@+id/ItemTitle"
- android:id="@+id/ItemText">
- </TextView>
- </RelativeLayout>
這裡改用 RelativeLayout,關鍵屬性只有兩個方向:
ItemTitle 帶上 android:layout_toRightOf="@+id/ItemImage",意思是它排在圖片右側;
ItemText 同樣有 layout_toRightOf="@+id/ItemImage" 靠右對齊圖片,再加上 layout_below="@+id/ItemTitle",於是就落在標題下方,形成「圖在左、標題在上、說明在下」的排法。
用 RelativeLayout 的好處是元件之間以參照的方式定位,不必為了直向堆疊而多包一層 LinearLayout,層級淺、效能也更好。
程式骨架與上一篇一致:先綁定 ListView,用 ArrayList 搭配 HashMap 組出十筆資料,這次每筆多放一個 ItemImage 放圖片資源 ID;接著交給 SimpleAdapter,指定 my_imageitem 作為版面,並用 new int[]{R.id.ItemImage, R.id.ItemTitle, R.id.ItemText} 把三個 key 對應到三個元件 id。順序一定要與 String 陣列一致,否則資料會填錯位置。
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //綁定XML中的ListView,作為Item的容器
- ListView list = (ListView) findViewById(R.id.MyListView);
- //生成動態數組,並且轉載數據
- ArrayList<HashMap<String, Object>> lstImageItem = 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.icon);//添加圖像資源的ID
- map.put("ItemTitle", "This is Title.....");
- map.put("ItemText", "This is text.....");
- lstImageItem.add(map);
- }
- //生成適配器的ImageItem <====> 動態數組的元素,兩者一一對應
- SimpleAdapter saImageItems = new SimpleAdapter(this, //沒什麼解釋
- lstImageItem, //數據來源
- R.layout.my_imageitem, //ListItem的XML實現
- //動態數組與ImageItem對應的子項
- new String[] {"ItemImage","ItemTitle", "ItemText"},
- //ImageItem的XML文件裡面的一個ImageView,兩個TextView ID
- new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});
- //添加並且顯示
- list.setAdapter(saImageItems);
- }
Android 登入驗證範例:帳號密碼與剩餘次數
登入驗證範例通常包含三個部分:輸入欄位、驗證邏輯與錯誤鎖定。以下範例以三次機會為例。 輸入與驗證 帳號欄位使用 EditText,密碼欄位設定 textPassword。
Android Fragment 入門完全指南(圖文)
Fragment 是什麼 Fragment 從 Android 3.0(API 11)開始引入。可以把 Fragment 想成 Activity 裡的一個模塊:它有自己的
Android 接入新浪微博、騰訊微博與人人網的 OAuth 2.0 實作
許多 Android 應用會需要接入社交平台的分享、登入或發文能力。本文以新浪微博、騰訊微博與人人網為例,說明整合時最關鍵的授權流程,以及如何取得後續呼叫 API 所需的
Android 本地化:讓應用適配不同語言環境
為什麼需要本地化?Android 應用可能會在不同地區、不同語言環境的裝置上執行。為了讓使用者更容易理解,應用中的文字、數字格式與其他資源最好能依語言環境調整。建立區域資