android tools命名空间中好用的几个属性

一、tools 命名空间的作用有哪些?

根据官方文档描述,根据其属性的功能类别,大致有三种主要功能:

  • xml中的错误处理
  • xml 预览
  • 资源压缩

说的通俗一点就是:

  • 减少或者避免黄线提示,让代码更清爽,让编译少报错
  • 让预览界面更灵活,可以随心所欲的定制预览视图
  • 压缩资源文件,降低APK体积。

注意:用tools设置的默认值,只在预览时有用,不会影响运行时。

二、如果使用?

在布局的XML文件的根元素下,添加这条语句:xmlns:tools="http://schemas.android.com/tools"

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF"

扫描二维码关注公众号,回复: 3845371 查看本文章

android:orientation="vertical"

xmlns:tools="http://schemas.android.com/tools"

tools:context=".MainActivity">

</RelativeLayout>

三、好用的几个属性:

1、tools:context

在布局文件中设置了tools:context,能够快速找到关联的activity。

这个属性通常设置在布局的XML文件的根元素,用来记录这个布局和哪个Activity相关。

这样在布局文件中,按住Ctrl+点击鼠标左键,可以定位到相关的Activity。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF"

android:orientation="vertical"

xmlns:tools="http://schemas.android.com/tools"

tools:context=".MainActivity">

</RelativeLayout>

2、tools:listitem

在GridView/ListView/RecycleView中设置了tools:listitem,能够快速找到这个列表控件对应的item样式。

<GridView

android:id="@+id/gridview"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:horizontalSpacing="@dimen/dp_21"

android:numColumns="5"

android:paddingLeft="@dimen/dp_39"

android:paddingRight="@dimen/dp_39"

android:paddingTop="@dimen/dp_43"

android:verticalSpacing="@dimen/dp_23"

tools:listitem="@layout/main_grid_item"/>

3、tools:text

在TextView/Button等控件中设置了tools:text,能够起到假数据预览效果。真正运行时,设置的文字不会显示在控件上。

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

tools:text="hahaha" />

注意:若tools:text 和 android:text 同时存在,在预览时会优先展示 tools:text。

参考资料链接:https://www.jianshu.com/p/2912bcba4465

猜你喜欢

转载自blog.csdn.net/Deaful/article/details/83543571