Android 开发之:Tools 属性
直到今天创建项目的时候, 出现tools:showIn="@layout/activity_main"才想起要学习记录下 Tools属性的相关使用。这个Tools 是如此好使用,我却未能善用,简直抱歉。它提高Android 应用开发的效率、还可以提高代码质量。
首先介绍布局文件中的tools 属性。
如果用 Android Studio 创建一个简单的示例项目,在生成的布局文件中会有这么一行内容:
xmlns:tools=”http://schemas.android.com/tools”
在** tools 命名空间下定义了一些属性就是我们要介绍的 tools 属性。 顾名思义,这些属性都是为编辑工具准备的,具体来说就是 Android Studio 和布局文件编译器(AAPT**)。 在开发程序的时候,通过这些属性来告诉编辑器一些额外的信息。
tools:ignore
这个属性是为** Lint 准备的,属性的取值为逗号分隔的 Lint 问题 ID**。告诉 Lint 在检测代码的时候,这些问题不用检测。例如:
<string name=”show_all_apps” tools:ignore=”MissingTranslation”>All</string>
tools:targetApi
这个属性也是为 Lint 准备的。和 Java 注解 @TargetApi 一样。取值可以为 Android 版本代号或者对应的整数值。例如:
<GridLayout tools:targetApi=”ICE_CREAM_SANDWICH” >
tools:locale
Lint 和 Android Studio 都会使用该属性。取值为 语言和地区缩写。如果设置该值为非英文类型,则 Studio 不会执行拼写检查。该属性通常出现在资源文件的根元素上。用来告诉 Lint 该文件应该用在那种语言环境中。 例如:values/strings.xml 文件 设置该属性为 es
<resources xmlns:tools=”http://schemas.android.com/tools” tools:locale=”es”>
上面的设置告诉工具,默认的文件夹中的资源为西班牙语而不是英语。
tools:context
Lint 和 Android Studio 都会使用该属性。出现在布局根元素中,用来告诉工具该布局文件和哪个 Activity 关联。这样在设计的时候,布局编辑器会使用相关联的 Activity 的 theme 来渲染该布局文件。取值为 manifests 文件中的 activity name 值一样。例如:
<android.support.v7.widget.GridLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” tools:context=”.MainActivity” … > </android.support.v7.widget.GridLayout>``` **tools:layout** 该属性由 Android Studio 使用。通常用在 <fragment> 元素中,用来告诉编辑器该 fragment 使用的是哪个布局文件。 ```code=xml <fragment android:name=”com.example.master.ItemListFragment” tools:layout=”@android:layout/list_content” />
tools:listitem / listheader / listfooter
Android Studio 使用该属性来渲染列表元素。可以用在 AdapterView 的子 View 中。例如 <ListView>、<GridView>、<ExpandableListView> 等。这样在设计的时候,编辑器可以用指定的内容来显示预览内容。
<ListView android:id=”@android:id/list” android:layout_width=”match_parent” android:layout_height=”match_parent” tools:listitem=”@android:layout/simple_list_item_2″ />
tools:showIn
Android Studio 使用该属性。通常用在被其他布局文件引用的<include> 布局文件中。告诉编辑器该布局文件用在另外一个布局文件中。例如
<?xml version=”1.0″ encoding=”utf-8″?> <TextView xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:text=”@string/hello_world” android:layout_width=”wrap_content” android:layout_height=”wrap_content” tools:showIn=”@layout/activity_main” />
tools:menu
Android Studio 使用该属性。用在布局文件的根元素中,告诉编辑器在 ActionBar 上的菜单内容。取值为定义的 menu 的 id,多个 id 用逗号分隔;还可以使用定义 menu 的 xml 文件的名字。
例如:
<?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:orientation=”vertical” android:layout_width=”match_parent” android:layout_height=”match_parent” tools:menu=”menu1,menu2″ />
tools:actionBarNavMode
Android Studio 使用该属性。用在布局文件的根元素中,告诉编辑器在 ActionBar 上的导航模式。取值为 “standard”、 “list” 和”tabs” 之一。
<?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:orientation=”vertical” android:layout_width=”match_parent” android:layout_height=”match_parent” tools:actionBarNavMode=”tabs” />
除了上面这些属性以外,标准的 Android 布局属性都可以当做 tools 属性来使用。布局编辑器用这些属性来选择布局文件。这些属性被称之为 “设计时布局属性”,他们只被布局编辑器使用,编译后的代码中不存在这些内容。
例如:常用于即时预览填充 tools:text="weixiong_wang"等(最常用,最好用)。
参考:
http://blog.chengyunfeng.com/?p=755&utm_source=tuicool&utm_medium=referral
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果
Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果 附录1简单介绍了Android开源的图片加载框架。在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide在加载过程中就把矩形图转换成圆形的,则需要在Glide之上引入一个开源项目:glide-transformationsglide-transformations在github上的项目主页是:https://github.com/wasabeef/glide-transformations 写一个例子说明。 package zhangphil.app; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import com.bu...
- 下一篇
Android开发常用框架汇总
作为一名程序猿,好的工具会让你在搬运工的道路上越走越远。以下框架是AC在开发过程中经常会使用到的一些好的框架。列在这里做一个小小的总结,包含但不限于此。 响应式编程 RxJava https://github.com/ReactiveX/RxJava RxAndroid https://github.com/ReactiveX/RxAndroid 消息通信 EventBus 组件间的消息通信 https://github.com/greenrobot/EventBus 注解框架 butterknife View注解框架 https://github.com/JakeWharton/butterknife dagger Android和Java的依赖注入框架 https://github.com/google/dagger 数据解析 Gson https://github.com/google/gson fastjson https://github.com/alibaba/fastjson 数据库 Realm 移动数据库SQLite&ORM替代者 https://github....
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果