2016年1月10日 星期日

[Android] GridView與GridLayout使用上的差異

GridView:
如果你手邊有一批圖像或data, 需要將他們放進格子狀的畫面呈現, 甚至在滑動頁面時需要刷新裡面的圖像/data, Ex:相簿的九宮格(thumbnails), 可以使用GridView. 它可以幫你做到重複使用及回收這些格位, 提升使用上的效能.
GridView是透過Adapter轉接器, 類似ListView的作法, 將手邊的資料們根據index, 指定至這些一格一格的位置. 當然, 你也可以定義一格裡面的內容, 同時包含圖像或文字, 做成一個複合的Item, 提供給Adapter來介接.

GridLayout:
適用於畫面的佈局配置, 類似LinearLayout. 可針對Android元件做不同比重分配,  Ex:計算機按鈕. 下方為google blog提出的兩個範例.

GridLayout範例

[Android] 新project出現android.support.v7.internal.app.WindowDecorActionBar錯誤

此時Android Studio會提示你修正重新Build project, 但路徑不對的話, 仍然會出現錯誤




解決方式style可以替換使用
(1) NoActionBar,
(2) 以Base.Theme.AppCompat.Light.DarkActionBar取代Theme.AppCompat.Light.DarkActionBar





原因是style內使用到Android 5.0 (SDK 21)以前的Android ActionBar style.

AppTheme繼承自Theme.AppCompat.Light.DarkActionBar style

AndroidManifest.xml於Application style使用AppTheme

而ActionBar於已經被ToolBar取代, 使用ToolBar通常styles樣式會設為NoActionBar, 並在需要放置的layout內定義ToolBar 元件

Android 5.0 style (v21)
NoActionBar
定義ToolBar元件

[Android] Fragment 及 v4 support Fragment差異

1. android.app.Fragment - 支援Android 3.0 (API 11)以上版本
2. android.support.v4.app.Fragment - 如需支援Android 3.0以前再使用


android.app.Fragment:
(1)使用FragmentManager操作Fragment時, 必需使用 getFragmentManager()取得android.app.FragmentManager

android.support.v4.app.Fragment:
(1)裝載的Activity必須extends FragmentActivity,
(2)使用FragmentManager操作Fragment時, 必需使用 getSupportFragmentManager()取得android.support.v4.FragmentManager