Android Tools Attributes,让布局设计所见即所得

2024-06-03 12:38

本文主要是介绍Android Tools Attributes,让布局设计所见即所得,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

2016-10-13 亦枫 技术鸟 技术鸟
技术鸟

NiaoTech

上谈【安卓】,下论【苹果】。以扯淡的态度,面对操蛋的技术,用幽默的语言,诠释开发的经典。



回复1024 随机给你一个惊喜


开发人员在设计Android Layout布局时,总会伴随着一些乱七八槽的困扰。比如,为了更加逼真的真实数据预览效果,我们在开发时会将TextView的text属性写上一些假数据,而当运行到模拟器或真机上时这些假数据就成了造成体验上甚至测试BUG的脏数据,又需要一一清除。再比如,我们想在XML的预览界面上就看到ListView的Item内容,而不是只有通过编译运行时才能查看。等等,诸如这些存在于开发Layout内容阶段的困扰,都可以通过Tools Attributes得以解决,不妨了解一下。

xmlns 定义

Android提供了一种特殊的tools命名空间,来帮助开发人员在开发Layout布局时使用tools属性解决实时预览等问题。这种属性在应用打包运行时将会被忽略,不产生任何影响。tools命名空间的URI为:http://schemas.android.com/tools,通常可将其定义在Layout的根容器中(在Android Studio中,可利用Live Temples快捷方式,输入toolsNs即可对应提示),如:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></RelativeLayout>

这里将Tools Attributes按照功能分为两种类别,一种是去除Lint提示的,一种是展示布局预览的,下面一一介绍相关属性的使用。

Lint 提示

tools:ignore

这个属性用于告诉Android Lint忽视某些xml警告,比如平时使用ImageView的时候都要加上这么一句:android:contentDescription="@null",否则便会出现黄色警告:[Accessibility] Missing contentDescription attribute on image,此时就可以使用tools:ignore属性忽视这个警告:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="contentDescription"/>

tools:targetApi

类似Java代码中的@TargetApi注解一样,指定API级别,可以使用数字level,也可以直接用API name,比如elevation属性只能在API 21及更高版本使用,使用tools:targetApi属性可以忽视版本的警告:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:targetApi="LOLLIPOP"/>

tools:locale

指定IDE当前的假定本地语言,可用在资源文件的根元素上,比如用在values/strings.xml上,避免一些拼写语法上的检查:

<resources xmlns:tools="http://schemas.android.com/tools"
    tools:local="es"><!-- string --><string name="name">nombre</string></resources>

布局预览

这里也分为两种,一种是替换标准的android命名空间的控件固有属性,举个例子:

<TextView
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="Samples"/>

上例中使用tools:text替换了android:text标准属性,在design窗口预览时可以看到TextView的text内容,而运行时则会被忽略。诸如此类,其他android标准属性均可被替换以达到预览效果。另一种就是非android标准属性了,下面一一说明。

tools:context

这个属性用在layout文件的根元素上,指明与当前layout相关联的Activity,从而在预览时使用Activity的主题(theme一般定义在Manifest文件中并且与activities联系在一起,而非layout)。可以使用Activity的全名,也可以利用manifest中定义的包名作为前缀:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></RelativeLayout>

tools:layout

这个属性主要用于标签中,指定预览时用的layout布局文件内容:

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout="@layout/fragment_content"/>

tools:listitem/listheader/listfooter

顾名思义,这三个属性可用于诸如ListView、GridView、ExpandableListView等AdapterView的子类中,实现内容布局的预览。注意:经实践,在Android Studio中已经无法达到这些元素的内容预览效果,但tools:listitem属性可以用在RecyclerView中,比如:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:listitem="@android:layout/simple_list_item_checked"/>

RecyclerView使用tools:listitem的前后对比预览效果如下:


tools:showIn

这个属性用在标签所包含的layout的根元素上,指定一个所在的布局文件,这样在被嵌套的layout的design视图中便可以预览到外层的layout内容。比如在activity_main.xml中使用<include>标签嵌套一个include_content.xml文件,那么在include_content.xml文件的根元素中就可以使用tools:showIn属性指定Outer layout,达到在include_content.xml文件中预览activity_main.xml内容的效果:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="@layout/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></RelativeLayout>

tools:menu

这个属性用在layout根元素中,指定layout预览时ActionBar展示的menu内容。使用tools:context属性时,ActionBar会自动查找Activity中的onCreateOptionsMenu()方法预览menu,tools:menu属性的设置会覆盖tools:context属性的效果。tools:menu的值只需要使用menu文件的名字即可,多个menu使用逗号隔开,如

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:menu="menu1,menu2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></RelativeLayout>

tools:actionBarNavMode

这个属性用在layout根元素中,指定layout预览时ActonBar的导航模式,值有standard、list和tabs三种,如:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:actionBarNavMode="standard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></RelativeLayout>

ActionBar不同navigation mode下的预览效果如图所示,关于navigation mode的相关信息可参考Top Level Switching With View Controls


tools:shrinkMode/keep/discard

为了使APK文件尽可能地变小,一般在打包发布时会开启Shrink Code和Shrink Resources的功能,删除项目中无用的代码和资源,使用这三个属性可以指定某些resources的保留与删除,具体信息可以参考官网介绍:Shrink Your Code and Resources

官方参考

以上便是笔者对于Android Tools Attributes的一些理解和实践,英文较好的也可以直接参考官方介绍,地址如下:

http://tools.android.com/tech-docs/tools-attributes


热门推文

Android 利用Gradle实现app的环境分离

Android Studio 使用Gradle引入第三方库文件的总结

ADB Idea,图形化界面,让ADB命令更简单一点

这「非常7+1」条小贴士,助你Coding水平再提一阶!

推荐|一张思维导图,告诉你Android新手如何快速入门




阅读原文 进入【YiFeng ' Zone】

重在分享,随心而赏~

赞赏

人赞赏

阅读原文

这篇关于Android Tools Attributes,让布局设计所见即所得的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1027011

相关文章

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

C#特性(Attributes)和反射(Reflection)详解

《C#特性(Attributes)和反射(Reflection)详解》:本文主要介绍C#特性(Attributes)和反射(Reflection),具有很好的参考价值,希望对大家有所帮助,如有错误... 目录特性特性的定义概念目的反射定义概念目的反射的主要功能包括使用反射的基本步骤特性和反射的关系总结特性