Android tools sample data: Make preview great again
在 Android 界面开发过程中我们经常需要查看页面效果。在远古时期使用 eclipse 开发的时候预览是一件让人头疼的事,代码和预览界面不能同时出现,需要切换 tab,好在 Android Studio 提供的编辑界面可以让我们同时看到代码和预览图,省去我们开会切换的时间。(这是当年驱使我把 eclipse 换成 Android Studio 的主要原因之一)
但是预览界面提供的预览比较弱,只能看一些简单的效果,稍微复杂的效果就需要运行起来才能看到,当工程变得复杂之后,构建一次 app 的时间会比较长,如果编写复杂界面,需要调整好几次的时候,花在构建上的时间可能都要很久,这会在一定程度上影响开发效率。相对于原生,react native 和 flutter 在这方面优势就很突出,只需要 reload 一下马上就能看到更新,不用每次去重新构建。虽然原生也有 instant run 的功能,但是开启之后可能会造成很多莫名其妙问题,所以大部分时候都会关掉 instant run 保平安。
android tools 属性
tools: 替换 android:
tools 属性能帮助我们增强预览效果。比如 tools:text
能够帮助我们预览文本,它的效果跟 android:text
一样,但是在实际运行中并不起作用。eg. 我们在编写 RecyclerView item 的时候需要预览效果,但是因为每个 item 的数据都不同,我们不能写死 android:text
。如果我们想要预览文本效果,我们可以在 xml 使用 android:text
, 在提交代码的时候删掉,但是这样会比较麻烦,也可能会忘记,这时候我们就可以使用 tools:text
来代替 android:text
,预览效果完全相同,并且我们即使不删除代码也不会对运行效果造成影响,可以放心大胆的 push 代码。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gaZxH1ML-1583573217857)(https://i.loli.net/2020/03/05/rHomafPRENxXe4y.png)]
实际上大部分 android:xxx
的属性都能替换成 tools:xxx
,这样我们就能在预览的时候看到显示效果,并且不用担心打包的时候不小心把测试数据带上去。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/imageView"android:layout_width="64dp"android:layout_height="64dp"android:layout_marginStart="16dp"android:layout_marginTop="16dp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"tools:ignore="ContentDescription"tools:src="@mipmap/ic_launcher" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"app:layout_constraintStart_toEndOf="@+id/imageView"app:layout_constraintTop_toTopOf="@+id/imageView"tools:text="Jon Snow"tools:textColor="#000"tools:textSize="20sp" /><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"app:layout_constraintStart_toStartOf="@+id/textView2"app:layout_constraintTop_toBottomOf="@+id/textView2"tools:text="I know nothing"tools:textColor="#666"tools:textSize="14sp" /><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="24dp"app:layout_constraintBottom_toBottomOf="@+id/imageView"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="@+id/imageView"tools:text="3/8/2020"tools:textColor="#666"tools:textSize="14sp" />
</androidx.constraintlayout.widget.ConstraintLayout>
tools:listitem
listitem 可能帮助我们预览 RecyclerView 的样式。通常情况下我们在 xml 中加入 RecyclerView 控件,as 会生成一个默认的预览样式.默认的样式很简单,我们不能看到实际的效果,需要运行代码才能看到。使用tools:listitem
可以帮助我们实现在编辑的时候预览 RecyclerView 的效果。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ui.main.MainFragment"><androidx.recyclerview.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"tools:listitem="@layout/test_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
除了 tools:listitem
,Android 还提供了另外的属性帮助我们更好的预览 RecyclerView。 tools:itemCount
可以设置预览个数,搭配 layoutManager
和 spanCount
我们还能预览 GridLayoutManager 下的样式。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ui.main.MainFragment"><androidx.recyclerview.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"tools:itemCount="10"tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"tools:listitem="@layout/test_avatar_item"tools:spanCount="3" />
</androidx.constraintlayout.widget.ConstraintLayout>
其他属性
除了上面介绍的属性之外,tools 还有很多别的作用,比如 tools:ignore
,tools:context
,tools:showIn
等,具体可以参考官方文档
sample data
使用 tools 属性后我们能够在一定程度上增强我们的预览效果,但是效果比较一般,比如预览 RecyclerView 的数据时,我们虽然可以使用 tools:listitem
和 tools:itemCount
设置预览数据,但是每个 item 都一样,数据看上去很假,跟实际使用的时候有一些出入。sample data 的出现能够很好的帮我解决这个问题,真正的实现 make preview great again。我们可以使用 as 内置的 sample data,也可以自定义数据,数据会体现在预览界面上,让我们的预览更接近实际运行效果。
as 内置 sample data
Android Studio 内置了一些 sample data,我们可以直接使用。
item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/imageView"android:layout_width="64dp"android:layout_height="64dp"android:layout_marginStart="16dp"android:layout_marginTop="16dp"android:layout_marginBottom="16dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"tools:ignore="ContentDescription"tools:src="@tools:sample/avatars" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"app:layout_constraintStart_toEndOf="@+id/imageView"app:layout_constraintTop_toTopOf="@+id/imageView"tools:text="@tools:sample/full_names"tools:textColor="#000"tools:textSize="20sp" /><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"app:layout_constraintStart_toStartOf="@+id/textView2"app:layout_constraintTop_toBottomOf="@+id/textView2"tools:text="@tools:sample/us_phones"tools:textColor="#666"tools:textSize="14sp" /><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="24dp"app:layout_constraintBottom_toBottomOf="@+id/imageView"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="@+id/imageView"tools:text="@tools:sample/date/mmddyy"tools:textColor="#666"tools:textSize="14sp" /></androidx.constraintlayout.widget.ConstraintLayout>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ui.main.MainFragment"><androidx.recyclerview.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"tools:itemCount="10"tools:listitem="@layout/test_sample_data_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
我们看一下预览效果是不是比之前的好一些呢,每个 item 都有不同的数据,而且我们也不需要引入额外的测试数据。
除了上面用到的 avatar, name 之类的,sample data 还有很多类型,基本上能满足我们的日程需求,具体含义可以参考官方文档。
自定义 sample data
如果 Android Studio 提供的 sample data 还不能满足你的需求,比如内置的姓名只有英文,你一定要预览中文名字,这时候可以自定义数据。
我们可以通过 new -> Sample Data directory
来创建数据目录。在 sampledata 下创建文本文件
mynames:
亚托克斯
阿狸
阿卡丽
阿利斯塔
阿木木
艾尼维亚
安妮
艾希
奥瑞利安·索尔
阿兹尔
巴德
布里茨
布兰德
mynicknames
暗裔剑魔
九尾妖狐
暗影之拳
牛头酋长
殇之木乃伊
冰晶凤凰
黑暗之女
寒冰射手
铸星龙王
沙漠皇帝
星界游神
蒸汽机器人
复仇焰魂
在 xml 中使用 tools:text="@sample/mynicknames"
即可。
除了上面这种简单文本的定义,我们还可以使用 json 来定义数据。
loldata.json:
{"comment": "lol data - loldata.json","data": [{"name": "亚托克斯","nickname": "暗裔剑魔"},{"name": "阿狸","nickname": "九尾妖狐"},{"name": "阿卡丽","nickname": "暗影之拳"},{"name": "阿利斯塔","nickname": "牛头酋长"},{"name": "阿木木","nickname": "殇之木乃伊"},{"name": "艾尼维亚","nickname": "冰晶凤凰"}]
}
在 xml 中使用 tools:text="@sample/loldata.json/data/name"
即可
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/imageView"android:layout_width="64dp"android:layout_height="64dp"android:layout_marginStart="16dp"android:layout_marginTop="16dp"android:layout_marginBottom="16dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"tools:ignore="ContentDescription"tools:src="@sample/myimg" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"app:layout_constraintStart_toEndOf="@+id/imageView"app:layout_constraintTop_toTopOf="@+id/imageView"tools:text="@sample/loldata.json/data/name"tools:textColor="#000"tools:textSize="20sp" /><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"app:layout_constraintStart_toStartOf="@+id/textView2"app:layout_constraintTop_toBottomOf="@+id/textView2"tools:text="@sample/loldata.json/data/nickname"tools:textColor="#666"tools:textSize="14sp" /><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="24dp"app:layout_constraintBottom_toBottomOf="@+id/imageView"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="@+id/imageView"tools:text="@tools:sample/date/mmddyy"tools:textColor="#666"tools:textSize="14sp" /></androidx.constraintlayout.widget.ConstraintLayout>
效果是不是还不错呢
再说点什么
google 一直在改进编辑界面的功能和体验,特别是 ConstraintLayout 推出之后,编辑和预览的功能更加强大,现在 Google 在各个大会上演示 ConstraintLayout 功能的时候基本都使用拖拽,特别是 ConstraintLayout 2.0 推出 MotionLayout 之后,编辑的功能变得更加强大,能够预览各种炫酷的动画。这些都能很好帮助开发者节省开发时间, make preview great again!
代码
https://github.com/LyCharlie/SampleDataDemo
参考文献
- Google I/O '18
- 官方文档
Android tools sample data: Make preview great again相关推荐
- android tools ignore,android tools属性引用
利用tools命名空间,android studio支持很多XML属性,当构建app时这些属性会被擦除,对APK的大小和运行时行为没有任何影响.请看官网. tools属性大致可以分为三类:1,错误处理 ...
- android tools是干什么的,Android 冷兵器 之 tools
前言 Android开发在所难免的就是UI的预览和调整,一般情况下都是直接run看效果,或者是使用AS的preview预览,但这同样带来个小问题,就是你的测试内容会跟随着代码被打包到apk中,如果没做 ...
- Android Tools 在线更新SDK Manager
收集整理Android开发所需的Android SDK.开发中用到的工具.Android开发教程.Android设计规范,免费的设计素材等. 欢迎大家推荐自己在Android开发过程中用的好用的工具. ...
- android中调用fft函数,J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data)...
J使用PCM数据在Android中转换FFT(JTransforms FFT in Android from PCM data) 我一直在玩这个游戏已经有一段时间了,我无法弄清楚我在这里要做的事情. ...
- 安卓开发问题之 Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication
博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 问题 这个问题出现在程序 Launcher3 运行中,系统端需要我这里修改 Launcher3 的一些东西,修改了给他, ...
- No cached version of com.android.tools.build:gradle:2.0.0 available for offline mode.
异常场景 从AS2.0升级到2.1,重新编译工程后,抛出了如下异常 Error:A problem occurred configuring root project 'AndroidStudioPr ...
- Could not find com.android.tools.build:gradle
Could not find com.android.tools.build:gradle:3.0.0. 或者改代码:根目录下的build.gradle buildscript {repositori ...
- 解决delphi10.2.3 android tools闪退
解决delphi10.2.3 android tools闪退 修改D:\Program Files (x86)\Embarcadero\Studio\19.0\PlatformSDKs\android ...
- com.android.tools.build:gradle:2.0.0-alpha3 build errors
当Android studio 编译时间过长且出现问题比如下面的提示: java.exe'' finished with non-zero exit value 3. 此时就要检查一下跟目录下面的bu ...
最新文章
- Ext.data.GroupingStore
- mysql跨库oracle查询_Oracle如何实现跨库查询
- NOIP2011 聪明的质监员
- dorado 刷新_dorado7常用内容
- 你真的搞懂ES6模块的导入导出规则了吗
- python3 文本处理_解决python3 写入中文文本查看为乱
- 1、matplotlib绘制一个简单的图形
- cvFilter2D() 卷积初步了解
- X86汇编语言经典资料,初学者必看(转)
- Redis Cluster集群实验
- 什么是域名服务器作用是啥,域名服务器的作用是什么?域名服务器原理及流程...
- Looking up JNDI object with name [LOGGING_PATTERN_LEVEL]
- PowerVR SDK
- Mackbook 外接移动硬盘无法写入数据(三种解决办法)
- 如何在公众号添加付费链接
- 【黑客攻防技术宝典】第4章 解析应用程序
- 6.3.3 延迟缓存
- 程序员又“作死”了,用AI算法一键“脱”衣,遭全球网友炮轰
- 数据结构(使用头插法实现单链表)
- Linux LVM(逻辑卷管理)
热门文章
- HTTP请求报文(请求行、请求头、请求体),响应报文
- SpringBoot 入门04
- 金融电商推广可行性活动概览
- java ssm旅游景点景区门票预订及信息发布系统
- 金融帝国实验室(Capitalism Lab)官方中文整合包(MOD模组/专业XGQ)_v8.0.15(2022.04.03更新)
- Less:Error evaluating function ‘unit‘ must be a number
- win平台改代码到android 平台需要注意
- 智慧社区平台建设应该从哪几个方面入手?
- 理化生数字化探究实验室建设及配置方案
- socket服务器探知客户端连接情况