[关闭]
@act262 2017-08-10T05:40:39.000000Z 字数 2034 阅读 1063

Effective Android Code

Training


代码检查

代码警告

编辑器面板的右侧可以查看代码“干净”程度,如果出现大量黄色的标记就需要注意了,如果是红色标记那就更大问题了。

注解相关

布局相关

使用tools命名空间

tools:ignore,抑制Lint的警告

  1. 常用到的几个属性
  2. tools:ignore="HardcodedText"
  3. tools:ignore="RtlHardcoded,RtlSymmetry"
  4. tools:ignore="ContentDescription"
  5. tools:ignore="UseCompoundDrawables"
  6. tools:ignore="ALL"

tools:context,关联到当前的Activity

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context="com.jfz.training1.BindThemeActivity">
  7. ...
  8. </LinearLayout>

扩展:

tools:xx,Design-time 属性,只作用在预览时,不会影响运行时

  1. 在运行时该按钮是GONE的,但是为了方便在预览时看到效果,使用对应的`tools:xx`属性来展示
  2. <Button
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="button"
  6. android:visibility="gone"
  7. tools:visibility="visible" />

tools:layout用在布局中fragment节点的预览效果

  1. <fragment
  2. android:name="com.jfz.training1.LayoutFragment"
  3. class="com.jfz.training1.LayoutFragment"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:layout="@layout/fragment_layout" />

tools:listitemtools:listheadertools:listfooter 列表布局的预览

常用在ListView、RecyclerView中做预览效果,listheader、listfooter在RecyvlerView中无效

  1. <ListView
  2. android:id="@android:id/list"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:dividerHeight="10dp"
  6. tools:listfooter="@layout/item_list_footer"
  7. tools:listheader="@layout/item_list_header"
  8. tools:listitem="@layout/item_list_type" />

注意:必须指定id才能有效果,listfooter可能看不到预览效果

tools:showIn用在被include的布局中做预览效果

实用的工具

参考:
https://developer.android.com/studio/write/tool-attributes.html
http://yanghui.name/blog/2015/08/31/tools-namespace-and-support-library-annotations/

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注