深入理解Android中的xmlns:tools属性_Android

前言

安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果。

但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了text。因此为了在ide中预览效果,你必须在xml中为TextView控件设置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"

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

一般我们在这样做的时候都告诉自己,没关系,等写完代码我就把这些东西一并删了。但是你可能会忘,以至于在你的最终产品中也会有这样的代码。

用tools吧,别做傻事

以上的情况是可以避免的,我们使用tools命名空间以及其属性来解决这个问题。

xmlns:tools=http://schemas.android.com/tools

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属性的种类

tools属性可以分为两种:一种是影响Lint提示的,一种是关于xml布局设计的。以上介绍的是tools的最基本用法:在UI设计的时候覆盖标准的android属性,属于第二种。下面介绍Lint相关的属性。

Lint相关的属性

tools:ignore

tools:targetApi

tools:locale

tools:ignore

ignore属性是告诉Lint忽略xml中的某些警告。

假设我们有这样的一个ImageView

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="@dimen/margin_main"

android:layout_marginTop="@dimen/margin_main"

android:scaleType="center"

android:src="@drawable/divider" />

Lint会提示该ImageView缺少android:contentDescription属性。我们可以使用tools:ignore来忽略这个警告:

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="@dimen/margin_main"

android:layout_marginTop="@dimen/margin_main"

android:scaleType="center"

android:src="@drawable/divider"

tools:ignore="contentDescription" />

tools:targetApi

假设minSdkLevel 15,而你使用了api21中的控件比如RippleDrawable

<ripple xmlns:android="http://schemas.android.com/apk/res/android"

android:color="@color/accent_color" />

则Lint会提示警告。

为了不显示这个警告,可以:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:color="@color/accent_color"

tools:targetApi="LOLLIPOP" />

tools:locale(本地语言)属性

默认情况下res/values/strings.xml中的字符串会执行拼写检查,如果不是英语,会提示拼写错误,通过以下代码来告诉studio本地语言不是英语,就不会有提示了。

<resources

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

tools:locale="it">

<!-- Your strings go here -->

</resources>

上面首先介绍了tools的最基本用法-覆盖android的属性,然后介绍了忽略Lint提示的属性。下面我们将继续介绍关于UI预览的其他属性(非android标准属性)。

注意:关于忽略Lint的属性,如果不想了解的话也没关系,因为并不影响编译,一般我都不会管这些警告。

这部分我们将继续介绍关于UI预览的其他属性(非android标准属性)。

  1. tools:context
  2. tools:menu
  3. tools:actionBarNavMode
  4. tools:listitem/listheader/listfooter
  5. tools:showIn
  6. tools:layout

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:menu

告诉IDE 在预览窗口中使用哪个菜单,这个菜单将显示在layout的根节点上(actionbar的位置)。

其实预览窗口非常智能,如果布局和一个activity关联(指上面所讲的用tools:context关联)它将会自动查询相关activity的onCreateOptionsMenu方法中的代码,以显示菜单。而menu属性则可以覆盖这种默认的行为。

你还可以为menu属性定义多个菜单资源,不同的菜单资源之间用逗号隔开。

tools:menu="menu_main,menu_edit"

如果你不希望在预览图中显示菜单则:

tools:menu=""

最后需要注意,当主题为Theme.AppCompat时,这个属性不起作用。

tools:actionBarNavMode

这个属性告诉ide  app bar(Material中对actionbar的称呼)的显示模式,其值可以是

standard

tabs

list
<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" />

同样的,当主题是Theme.AppCompat (r21+, at least) 或者Theme.Material,或者使用了布局包含Toolbar的方式。  该属性也不起作用,只有holo主题才有效。

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" />

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" />

tools:showIn

该属性设置于一个被其他布局<include>的布局的根元素上。这让您可以指向包含此布局的其中一个布局,在设计时这个被包含的布局会带着周围的外部布局被渲染。这将允许您“在上下文中”查看和编辑这个布局。需要 Studio 0.5.8 或更高版本。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, tools
, xmlns
, xmlns属性
tools的作用
,以便于您获取更多的相关知识。

时间: 2024-08-17 20:02:18

深入理解Android中的xmlns:tools属性_Android的相关文章

深入理解Android中的xmlns:tools属性

前言 安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果. 但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了text.因此为了在ide中预览效果,你必须在xml中为TextView控件设置android:text属性 <TextView android:id="@+id/text_main" android:layout_width="match_parent" andro

深入理解Android中Scroller的滚动原理_Android

View的平滑滚动效果 什么是实现View的平滑滚动效果呢,举个简单的例子,一个View从在我们指定的时间内从一个位置滚动到另外一个位置,我们利用Scroller类可以实现匀速滚动,可以先加速后减速,可以先减速后加速等等效果,而不是瞬间的移动的效果,所以Scroller可以帮我们实现很多滑动的效果. 首先我们先来看一下Scroller的用法,基本可概括为"三部曲": 1.创建一个Scroller对象,一般在View的构造器中创建: public ScrollViewGroup(Cont

理解Android中Activity的方法回调_Android

为什么需要方法回调? 方法回调是功能定义和功能分离的一种手段,是一种松耦合的设计思想.在JAVA中回调是通过接口来实现的.作为一种系统架构,必须要有自己的运行环境,并且要提供用户的实现接口. 下面通过实例来模拟一下Android中Activity的方法回调思想.Activity接口 复制代码 代码如下: package com.xujing.test  //定义接口  public interface Activity{      //创建时调用的方法      public void onCr

深入理解Android中的Handler异步通信机制_Android

一.问题:在Android启动后会在新进程里创建一个主线程,也叫UI线程(非线程安全)这个线程主要负责监听屏幕点击事件与界面绘制.当Application需要进行耗时操作如网络请求等,如直接在主线程进行容易发生ANR错误.所以会创建子线程来执行耗时任务,当子线程执行完毕需要通知UI线程并修改界面时,不可以直接在子线程修改UI,怎么办? 解决方法:Message Queue机制可以实现子线程与UI线程的通信. 该机制包括Handler.Message Queue.Looper.Handler可以把

深入理解Android中的建造者模式_Android

前言 在Android开发过程中,我发现很多安卓源代码里应用了设计模式,比较常用的有适配器模式(各种adapter),建造者模式(Alert Dialog的构建)等等.虽然我们对大多数设计模式都有所了解,但是在应用设计模式的这个方面,感觉很多人在这方面有所不足.所以这篇文章我们一起深入的理解Android中的建造者模式. 建造者模式(Builder Pattern)也叫生成器模式,其定义如下: separate the construction of a complex object from

深入理解Android中View和ViewGroup

深入理解Android中View 这回我们是深入到View内部,去研究View,去了解View的工作,抛弃其他因素,以便为以后能灵活的使用自定义空间打下一定的基础.希望有志同道合的朋友一起来探讨,深入Android内部,深入理解Android. 一.View是什么? View是什么了,每个人都有自己的理解.在Android的官方文档中是这样描述的:这个类表示了用户界面的基本构建模块.一个View占用了屏幕上的一个矩形区域并且负责界面绘制和事件处理.View是用来构建用户界面组件(Button,T

Android菜单详解——理解android中的Menu

Android菜单详解--理解android中的Menu 前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至今为止看到的最好的一本android书,中文版出到<精通Android 2>. 理解Android的菜单 菜单是许多应用程序不可或缺的一部分,Android中更是如此,所有搭载Android系统的手机甚至都要有一个"Menu&qu

深入理解Android中的建造者模式

前言 在Android开发过程中,我发现很多安卓源代码里应用了设计模式,比较常用的有适配器模式(各种adapter),建造者模式(Alert Dialog的构建)等等.虽然我们对大多数设计模式都有所了解,但是在应用设计模式的这个方面,感觉很多人在这方面有所不足.所以这篇文章我们一起深入的理解Android中的建造者模式. 建造者模式(Builder Pattern)也叫生成器模式,其定义如下: separate the construction of a complex object from

如何理解android中的api level

问题描述 如何理解android中的api level 刚开始是api level 1 现在api level 多少了 是不是随着android版本的更新而不断的更新 解决方案 可以查看android api的官网,http://source.android.com/source/build-numbers.html level是会随版本更新的,现在都二十几了,应该是23了吧 解决方案二: http://blog.csdn.net/nihaoqiulinhe/article/details/50