tools:context用法

tools可以告诉Android Studio,哪些属性在运行的时候是被忽略的,只在设计布局的时候有效。比如我们要让android:text属性只在布局预览中有效可以这样

<TextView

 android:id="@+id/text_main"

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:textAppearance="@style/TextAppearance.Title"

 android:layout_margin="@dimen/main_margin"

 tools:text="I am a title" />

tools可以覆盖android的所有标准属性,将android:换成tools:即可。同时在运行的时候就连tools:本身都是被忽略的,不会被带进apk中。

其中的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>

tools 相关的属性是提示给编辑器的,也就是用来辅助编辑器展示效果,在真机上这些属性是没有作用的。例如这里的tools:context 就是将这个 layout 文件和后面的 Activity 进行关联,这样编辑器在展示布局效果的时候,就能针对Activity 的一些属性进行有针对性的处理。

相关文章

学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习...
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面...
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生...
Can’t connect to local MySQL server through socket \'/v...
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 ...
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服...