以前写代码没注意过tools有啥功能,只知道把这个删除了,对代码运行没啥影响,所以就没有管这个是啥了!
这几天看见大神在用这个功能,觉得挺神奇的,所以就看了下网上的资料!
如:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0309/2567.html
个人理解:使用tools是为了在布局中给人更好的预览效果,不必自己去填充假数据,然后等项目上线了再去删掉这些假的数据代码.工程之大!
所以:使用tools命名空间以及其属性来解决这个问题。
其原理为:tools可以覆盖android的所有标准属性,将android:换成tools:即可。同时在运行的时候就连tools:本身都是被忽略的,不会被带进apk中,而使用android的时候,编译会将这部分的资源带进apk中
//其命名空间
xmlns:tools="http://schemas.android.com/tools"
1,例如:使用TextView控件时
//丢弃写法
<TextView android:text = "填充假的数据">
//正确写法为
<TextView tools:text = "填充假的数据">
2,使用ImagView显示图片:
本来想隐藏图片的,但是又想知道图片的具体位置,所以既可使用:
//既可看到隐藏后的图片效果
<ImagView android:visibility = "GONE"
/*----------------*/
tools:visibility = "visible">
3,tools:layout属性
tools:layout告诉ide,Fragment在程序预览的时候该显示成什么样
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_list"
android:name="com.example.fragmenttwopanel.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
/*----------------*/
tools:layout="@android:layout/list_content" />
4.tools:listitem, listheader 和listfooter 属性
顾名思义就是在ListView ExpandableListView等的预览效果中添加头部 尾部 以及子item的预览布局。
<GridView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/*----------------*/
tools:listheader="@layout/list_header"
tools:listitem="@layout/list_item"
tools:listfooter="@layout/list_footer" />
5,tools:context
context属性其实正是的称呼是activity属性,有了这个属性,ide就知道在预览布局的时候该采用什么样的主题。同时他还可以在android studio的java代码中帮助找到相关的文件(Go to Related files),
且:该属性的值是activity的完整包名
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/*----------------*/
tools:context="com.android.example.MainActivity"> <!-- ... -->
</LinearLayout>
以上!目前本人在项目中,只使用了以上这几点,欢迎大家补充