前言
Android开发在所难免的就是UI的预览和调整,一般情况下都是直接run看效果,或者是使用AS的preview预览,但这同样带来个小问题,就是你的测试内容会跟随着代码被打包到apk中,如果没做容错的处理很有可能将测试内容展示给用户。
还有就是像一些列表是不支持预览效果的,比如ListView。
其实Android老早就有tools命名空间,作为一个开发快到一年的我,最近看到一篇文章才发现这个tools大用处,在此做个笔记。
直接看一下效果,最为清楚:
RecyclerView.gif
ListView 预览.png
GridView 预览.png
XML中的代码区区几行,但是右侧的preview效果却很直接,还有就是这些数据并不会打到apk中,直接运行是没有任何效果的,这就是厉害之处!相关代码已提交到Github上: Android Tools
初步使用
既然是命名空间,那肯定就要在XML开头就要声明。
xmlns:tools="http://schemas.android.com/tools"
类似于
xmlns:android="http://schemas.android.com/apk/res/android"
然后就可以使用tools的相关属性了。属性功能很多很全,我就介绍一下常用的,文尾会贴上相关的文章链接。
View 相关
先从view相关说起,在XML中view的android任意属性值,可以直接替换成tools,这样就可以实现实时的预览效果,并且正式部署之后不会展示。有点类似于dataBinding中的 def