2014/04/17

View.isInEditMode()

今天參加 Android 高雄開發者社群聚會,聽到了一個好用的 method,當你在使用自己的 Custom View 時,常會碰到無法在 GUI Design 的介面中正常顯示的問題,並且會看到下面這段提示,

Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE

因為預覽時只會根據 xml layout 建立物件但不會初始化,所以 context  會是 null,這時只要在會用到 context 的地方運用這個 method,就可以正常顯示 UI 了。

沒有留言:

張貼留言