Open
Conversation
Author
|
Theme.BLTheme 写错了,应该是 Theme.Background.Preview |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
支持 Android Studio 布局预览功能
概述
本 PR 添加了 Android Studio 布局预览器(Layout Preview)的支持,使开发者可以在 IDE 中实时预览
bl_*属性的效果,无需运行应用即可查看背景渲染结果。与旧版预览方式的区别
BLTextView、BLButton等)新版预览方式的优势:相比旧版必须将控件替换为
BLTextView、BLButton等自定义控件的方式,新版只需切换主题即可实现预览,无需修改布局文件中的任何控件标签,极大降低了使用成本和迁移难度。主要变更
1. 新增自定义 ViewInflater
BackgroundViewInflater类(分别支持library和libraryx模块)AppCompatViewInflater,在编辑模式下劫持视图创建过程,解析bl_*属性2. 新增预览支持的自定义控件
BLAutoCompleteTextViewBLCheckedTextViewBLMultiAutoCompleteTextViewBLRatingBarBLSeekBarBLSpinner3. 新增主题样式配置
styles.xml,配置主题以启用自定义 ViewInflater4. Gradle 版本更新
技术实现
通过自定义
AppCompatViewInflater并在主题中配置viewInflaterClass,当布局编辑器处于编辑模式时,自动注入背景解析逻辑,实现预览效果。使用方式
在布局预览器中选择
Theme.BLTheme主题(或在AndroidManifest.xml/styles.xml中配置),即可在布局预览器中看到bl_*属性的渲染效果。注意事项
由于 Android Studio 布局预览器(Layout Preview)对全域名控件(如
androidx.appcompat.widget.AppCompatTextView)的处理局限性,该方法必须要先加载一个短域名的标准控件用以触发预览劫持(如View、LinearLayout、FrameLayout等),在这之后渲染的控件才会自动预览生效。建议:在布局文件顶部添加一个不可见的短包名控件作为触发器: