2016/07/13

android 中的 Layoutinflator.inflate 的 attachToRoot?

Android 裡的 Layoutinflater 可以用來把 XML 轉換成 ViewGroup

通常拿來用在 Fragment 的 onCreateView() 方法裡
一般來說都是使用下面這個 inflate 方法


inflate
(int resource, ViewGroup root, boolean attachToRoot)
Inflate a new view hierarchy from the specified xml resource.

現在分析兩種情況,

attachToRoot 設為 true 的時候,代表 View 時直接被 attach 到 root group 裡,而且會回傳 root view

inflater.inflate(R.layout.custom_button, mLinearLayout, true);


attachToRoot 設為 false 的時候代表 這個 inflate 出來的 View  不是馬上被 attach 到 ViewGroup,而是指說在未來的某一個時間點才會,這方法會回傳自己的 view。

例如先將 XML inflate 後,到某個時間點在自己加進 layout
Button button = (Button) inflater.inflate(R.layout.custom_button, mLinearLayout, false);
mLinearLayout.addView(button);

最常見的例子就是 ListView (RecyclerView),因為是由 RecyclerView 來控制什麼時間點才要將 View 加入

探討 View.inflate

另外如果我們是直接使用 View.inflate ,他的本質其實就是直接將 inflate 出來的加到 layout 裡
例如下面這樣其實等同於 Layoutinflator.inflate 並且帶 attachRoot = true
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
View view = View.inflate(this, R.layout.layout_menu_item, layout);
後記:如果你的 XML root 是 就只能 attachToRoot = true 否則會出現 InflateException

Caused by: android.view.InflateException: can be used only with a valid ViewGroup root and attachToRoot=true

參考:https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/

2016/07/08

Android Studio 快速鍵 on MAC

1. Outline of Class  ( Command + 12)

2. Call Hierarchy (Ctrl + Alt + h)

3. Toggle Bookmark (F3)

4. Show bookmarks (Command + F3)

5. Find Action (Command + Shift + a)

6. Move Line (Alt + Shift + Up/Down)

7. Version Control Operation Popup (Ctrl + V)

8. Close All Panel (Shift + F12)

9. Duplicate Line (Command + D)

10. Rename (Shift + F6)

11. Select Multiple Line (Alt + Mouse Selection)

12. Toggle Breakpoint (Alt + F8)

2016/03/06

Android draw9patch 最簡單解釋

網路上一堆 draw9patch 的教學但是都看不懂

比較容易懂的大概只有 Intel 的這個
https://software.intel.com/en-us/xdk/articles/android-splash-screens-using-nine-patch-png

draw9patch 說穿了只有一個目的:「保留不要延伸的部分」,也可以換句話說是「選擇要延伸的部分」,就看個人喜歡怎樣理解。

實際快速看一段操作影片,可以看到一開始點選邊邊透明的部分來決定切割點,最後再把切割點拉開,填滿未來想要自動延伸的部分 (也可以換句話說就是不要拉到不想延伸的部分)



教學使用的圖片是從前面的網址中取得的

圖片來源:取自 intel.com

本文是使用 android sdk 內建的 draw9patch

聽說設計師都是用 9 patch generator
https://romannurik.github.io/AndroidAssetStudio/nine-patches.html

2016/03/05

手動 Zero Down Time Laravel Deploymenet

目錄結構

web-app
|-- current  (symbol-link to laravel-app-ver-01)
|-- laravel-app-ver-01
|-- .env
|-- storage
|-- cache

必要的基本設置
*nginx 先設定指向 web-app/current/public
*.env 跟 storage 放在 web-app 根目錄, laravel-app-ver01 內則是直接 link 他們

接下來開始佈署流程

working dir: web-app

1. git clone your-laravel-app
2. mv your-laravel-app laravel-app-ver02

這時候目錄下會長這樣

web-app
|-- current (symbol-link to laravel-app-ver-01)
|-- laravel-app-ver-01
|-- laravel-app-ver-02
|-- .env
|-- storage
|-- cache

3. cd laravel-app-ver-02

# checkout 到你要 deploy 的 branch
4. git checkout --track -b release origin/release

# 刪除 storage 資料夾
5. rm -rf storage

# 把需要的檔案 link 進來 (甚至 node_modules 也可以 link ,可減少 npm install 的時間)
6. ln -s web-app/storage storage
7. ln -s web-app/.env .env
8. ln -s web-app/bootstrap/cache cache

# 剩下的是看需要什麼就跑什麼
composer update --no-scripts
npm install
gulp
php artisan migrate

最後要做的就是把 current link 抽換成 laravel-app-ver-02

然後就可以砍掉 laravel-app-ver01 了

// 不知道為什麼 overwrite 的時候要反過來...
// 可以觀察到前面的 rules 都是 A -> B 的時候參數順序是 A, B
// 但是當要讓 current -> laravel-app-ver-02 的時候卻是帶 B, A
ln -snf laravel-app-ver-02 current

※ 以上的動作可以直接用 envoyer 做掉,一個月 10美金。

2016/03/02

Mac 安裝 nvm (不用sudo 不用 homebrew)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

source ~/.nvm/nvm.sh

nvm ls-remote

nvm install v5.7

nvm use 5.7

nvm alias default 5.7