本文共 1912 字,大约阅读时间需要 6 分钟。
根据官方文档描述,根据其属性的功能类别,大致有三种主要功能:
说的通俗一点就是:
注意:用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"
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。
参考资料链接:
转载地址:http://mpsqf.baihongyu.com/