android 布局preview 技巧

来源:互联网 发布:成都软件行业协会名录 编辑:程序博客网 时间:2024/05/17 03:03

最近开始看老外写的文章,博客,嗯,不能说比国人写的好,但是感觉看着很爽。真的,一手资料就是爽。
嗯,自己做得不错,第一次看外文博客,我知道自己怎么看的,一句话一句话看下来的,越看越有感觉。
下面这个
https://www.novoda.com/blog/layout-preview-101/

介绍了编写布局时候的技巧,我总结如下:
1.tools 属性:

By using the tools namespace instead of android, while declaring xml attributes, allows you to specify attributes that are going to be used only while in preview. In this case we use tools:text="Title" and tools:src="@drawable/cool_pic"

tools:text 而不是android:text,保证了你所有的内容都只是在预览的时候会出现,程序运行起来不会出现所有tools相关的东西。
包括

tools:visiable

你想显示隐藏的布局,不想来回改代码,这个tools是个不错的选择。

2.自定义控件 preview的时候显示不出来:

In this case View.isInEditMode() saves the day. Use it to check whether you are running on the Preview tool and skip any initialisation that requires dependencies that aren’t available at design time:    public ImageWithCaptionView(Context context, AttributeSet attrs) {        super(context, attrs);        if (!isInEditMode()) {            ArticlesApplication.getInjector().inject(this);        }    }

不错,因为中文的博客我都看烦了,所以去看外文。感觉很不错。

0 0
原创粉丝点击