微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 的一些属性进行有针对性的处理。

原文地址:https://cloud.tencent.com/developer/article/2178549

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐