設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 437|回復: 0
打印 上一主題 下一主題

Android 中xml tools属性详解

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2020-4-27 06:28:02 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
第一部分安卓开发中,在写布局代码的时候,AndroidStudio可以看到布局的预览效果。
但是有些效果则必须在运行之后才能看见,
比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了text。
因此为了在AndroidStudio中预览效果,你必须在xml中为TextView控件设置android:text属性
  1. <TextView
  2.   android:id="@+id/text_main"
  3.   android:layout_width="match_parent"
  4.   android:layout_height="wrap_content"
  5.   android:text="I am a title" />
複製代碼
一般我们在这样做的时候都告诉自己,没关系,等写完代码我就把这些东西一并删了。但是你可能会忘,以至于在你的最终产品中也会有这样的代码。
用tools吧,别做傻事!!!
以上的情况是可以避免的,我们使用tools命名空间以及其属性来解决这个问题。
  1. <pre class="line-numbers  language-bash"><code class="  language-bash">xmlns:tools="http://schemas.android.com/tools"</code></pre>
複製代碼
tools可以告诉Android Studio,哪些属性在运行的时候是被忽略的,只在设计布局的时候有效。
比如我们要让android:text属性只在布局预览中有效可以这样
  1. <TextView
  2. android:id="@+id/text_main"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. tools:text="I am a title" />
複製代碼
tools属性的种类
tools属性可以分为两种:一种是影响Lint提示的,一种是关于xml布局设计的。
以上介绍的是tools的最基本用法:在UI设计的时候覆盖标准的android属性,属于第二种。

下面介绍Lint相关的属性。
Lint相关的属性
tools:ignore
tools:targetApi
tools:locale
tools:ignore
ignore属性是告诉Lint忽略xml中的某些警告。
假设我们有这样的一个ImageView
  1. <ImageView
  2.   android:layout_width="wrap_content"
  3.   android:layout_height="wrap_content"
  4.   android:layout_marginStart="@dimen/margin_main"
  5.   android:layout_marginTop="@dimen/margin_main"
  6.   android:scaleType="center"
  7.   android:src="@drawable/divider"
  8.   tools:ignore="contentDescription" />
複製代碼
Lint会提示该ImageView缺少android:contentDescription属性。我们可以使用tools:ignore来忽略这个警告
  1. <ImageView
  2.   android:layout_width="wrap_content"
  3.   android:layout_height="wrap_content"
  4.   android:layout_marginStart="@dimen/margin_main"
  5.   android:layout_marginTop="@dimen/margin_main"
  6.   android:scaleType="center"
  7.   android:src="@drawable/divider"
  8.   tools:ignore="contentDescription" />
複製代碼
tools:targetApi
假设minSdkLevel 15,而你使用了api21中的控件比如RippleDrawable
  1. <RelativeLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"      
  9.     android:paddingBottom="@dimen/activity_vertical_margin"                 
  10.     tools:context=".MainActivity">
  11. <TextView   
  12.     android:text="@string/hello_world"
  13.     tools:text="hello world"
  14.     android:layout_width="wrap_content"
  15.     tools:layout_width="400dp"
  16.     tools:background="@android:color/holo_red_dark"
  17.     android:layout_height="wrap_content"/>
  18. </RelativeLayout>
複製代碼
则Lint会提示警告。
为了不显示这个警告,可以:
  1. <ripple xmlns:android="http://schemas.android.com/apk/res/android"
  2.   xmlns:tools="http://schemas.android.com/tools"
  3.   android:color="@color/accent_color"
  4.   tools:targetApi="LOLLIPOP" />
複製代碼
tools:locale(本地语言)属性
默认情况下res/values/strings.xml中的字符串会执行拼写检查,如果不是英语,会提示拼写错误,通过以下代码来告诉studio本地语言不是英语,就不会有提示了。
  1. <resources
  2.   xmlns:android="http://schemas.android.com/apk/res/android"
  3.   xmlns:tools="http://schemas.android.com/tools"
  4.   tools:locale="it">

  5.   <!-- Your strings go here -->

  6. </resources>
複製代碼

这篇文章首先介绍了tools的最基本用法-覆盖android的属性,然后介绍了忽略Lint提示的属性。下篇文章中,我们将继续介绍关于UI预览的其他属性(非android标准属性)。
ps:关于忽略Lint的属性,如果不想了解的话也没关系,因为并不影响编译,一般我都不会管这些警告。
第二部分
这部分我们将继续介绍关于UI预览的其他属性(非android标准属性)。
tools:context
tools:menu
tools:actionBarNavMode
tools:listitem/listheader/listfooter
tools:showIn
tools:layout
tools:context
context属性其实正是的称呼是activity属性,有了这个属性,ide就知道在预览布局的时候该采用什么样的主题。
同时他还可以在android studio的java代码中帮助找到相关的文件(Go to Related files)
该属性的值是activity的完整包名
  1. <LinearLayout
  2.   xmlns:android="http://schemas.android.com/apk/res/android"
  3.   xmlns:tools="http://schemas.android.com/tools"
  4.   android:id="@+id/container"
  5.   android:layout_width="match_parent"
  6.   android:layout_height="match_parent"
  7.   android:orientation="vertical"
  8.   tools:context="com.android.example.MainActivity">  <!-- ... -->
  9. </LinearLayout>
複製代碼

tools:menu
Android Studio 使用该属性。用在布局文件的根元素中,告诉编辑器在 ActionBar 上的菜单内容。取值为定义的 menu 的 id,多个 id 用逗号分隔;还可以使用定义 menu 的 xml 文件的名字。例如:
  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout
  3.     xmlns:android=”http://schemas.android.com/apk/res/android”
  4.     xmlns:tools=”http://schemas.android.com/tools”
  5.     android:orientation=”vertical”
  6.     android:layout_width=”match_parent”
  7.     android:layout_height=”match_parent”
  8.     tools:menu=”menu1,menu2″ />
複製代碼
其实预览窗口非常智能,如果布局和一个activity关联(指上面所讲的用tools:context关联)它将会自动查询相关activity的onCreateOptionsMenu方法中的代码,以显示菜单。
而menu属性则可以覆盖这种默认的行为。
你还可以为menu属性定义多个菜单资源,不同的菜单资源之间用逗号隔开。
  1. tools:menu="menu_main,menu_edit"
複製代碼

如果你不希望在预览图中显示菜单则:
tools:menu=""
最后需要注意,当主题为Theme.AppCompat时,这个属性不起作用。
tools:actionBarNavMode
Android Studio 使用该属性。用在布局文件的根元素中,告诉编辑器在 ActionBar 上的导航模式。取值为 “standard”、 “list” 和”tabs” 之一。
  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout
  3.     xmlns:android=”http://schemas.android.com/apk/res/android”
  4.     xmlns:tools=”http://schemas.android.com/tools”
  5.     android:orientation=”vertical”
  6.     android:layout_width=”match_parent”
  7.     android:layout_height=”match_parent”
  8.     tools:actionBarNavMode=”tabs” />
複製代碼
standard
tabs
list
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:orientation="vertical"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     tools:actionBarNavMode="tabs" />
複製代碼

同样的,当主题是Theme.AppCompat (r21+, at least) 或者Theme.Material,或者使用了布局包含Toolbar的方式。  该属性也不起作用,只有holo主题才有效。
listitem, listheader 和listfooter 属性
顾名思义就是在ListView ExpandableListView等的预览效果中添加头部 尾部 以及子item的预览布局。
  1. <GridView
  2. android:id="@+id/list"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. tools:listheader="@layout/list_header"
  6. tools:listitem="@layout/list_item"
  7. tools:listfooter="@layout/list_footer" />
複製代碼

layout属性
该属性由 Android Studio 使用。通常用在 <fragment> 元素中,用来告诉编辑器该 fragment 使用的是哪个布局文件。
  1. <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:id="@+id/item_list"
  4.     android:name="com.example.fragmenttwopanel.ItemListFragment"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:layout_marginLeft="16dp"
  8.     android:layout_marginRight="16dp"
  9.     tools:layout="@android:layout/list_content" />
複製代碼
tools:showIn
Android Studio 使用该属性。通常用在被其他布局文件引用的<include> 布局文件中。告诉编辑器该布局文件用在另外一个布局文件中。例如
  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <TextView
  3.      xmlns:android=”http://schemas.android.com/apk/res/android”
  4.      xmlns:tools=”http://schemas.android.com/tools”
  5.      android:text=”@string/hello_world”
  6.      android:layout_width=”wrap_content”
  7.      android:layout_height=”wrap_content”
  8.      tools:showIn=”@layout/activity_main” />
複製代碼

除了上面这些属性以外,标准的 Android 布局属性都可以当做 tools 属性来使用。布局编辑器用这些属性来选择布局文件。这些属性被称之为 “ **设计时布局属性 **”,他们只被布局编辑器使用,编译后的代码中不存在这些内容。
例如
  1. <RelativeLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"      
  9.     android:paddingBottom="@dimen/activity_vertical_margin"                 
  10.     tools:context=".MainActivity">
  11. <TextView   
  12.     android:text="@string/hello_world"
  13.     tools:text="hello world"
  14.     android:layout_width="wrap_content"
  15.     tools:layout_width="400dp"
  16.     tools:background="@android:color/holo_red_dark"
  17.     android:layout_height="wrap_content"/>
  18. </RelativeLayout>
複製代碼

上面的布局文件,在布局编辑器中预览图如下:



这样做的好处是,在编辑布局文件的时候,不用为了预览效果而给真正的属性设置一些测试的值,然后当程序发布的时候,这些测试的内容忘记删除了。例如为了预览 EditText 的显示效果,你把一段文字设置到 text属性上:<EditText android:text="John Doe" android:layout_width="wrap_content" android:layout_height="wrap_content" />
当你发布项目的时候,却忘记了把该字符串给删除了。 如果这里面包含有比较机密的内容的话,你的秘密就被泄露出去了。 你可以用**tools:text="John Doe" **
这样在发布的时候,这些内容会自动去掉。
设计时布局属性的限制:
目前只支持标准的属性,也就是依 android 命名空间下的属性。
目前代码提示支持的还不是很好,建议先用 android 命名空间来编辑该属性,然后把 android 替换为 tools。
设计时布局属性只能在布局文件中使用,不能在其他资源文件中使用。

source链接:https://www.jianshu.com/p/7ef2c69e941f















本帖子中包含更多資源

您需要 登錄 才可以下載或查看,沒有帳號?立即註冊

x
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-6-12 02:08 , Processed in 0.210720 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表