android 布局xml文件中的 tools 属性




在Android studio中,xml的布局文件大多数使用的是android

android:id=函数

android:layout_marginLeft=布局

android:text=spa

...xml

等属性utf-8

这些属性前面的android关键字,实际上是对应了xml布局文件中的:ci

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

这个是命名空间声明,xmlns是XML Namespaces的缩写,中文名称是XML(标准通用标记语言的子集)命名空间,用来防止命名冲突的


那么,xmlns:tools="http://schemas.android.com/tools"这个tools属性有什么用呢?


以下代码,是一个时间显示linearlyout布局,显示相似 3:58:0 的时间,

用了3个TextView,在关联的acvitity文件中,这3个TextView显示的内容,都使用了开发

TextView.setText函数来动态设置更新获取到的时间。it

可是在开发过程当中,须要在xml右侧的界面预览中,观察这3个TextView的位置是否合适,因而会在xml中,3个textView属性中分别加入io

android:text="10",
android:text="20",
android:text="30",
以此能够在界面预览中观察到位置:


那么问题来了,这样的话,好比我在activity中设定初始显示时间是  3:58:10,那么在手机中运行的时候,activity中的oncreate()方法会初始化这个时间layout

这样,一开始的时候手机界面上显示的时间就会是 10:20:30,而后过一小会,才会显示变为在activity具体的时间获取函数中设定的 3:58:10。



有什么办法解决呢,用tools

tools能够告诉android studio,哪些xml布局属性只是在界面预览的时候显示(方便开发布局位置),而在真正运行的时候(例如在手机上运行),而不会显示该属性。
咱们能够在layout的xml文件中加入这句,

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

而后在3个textview的属性中的

android:text="10",
android:text="20",
android:text="30",
改为

tools:text="10",

tools:text="20",
tools:text="30",
这样的话,在界面预览中能显示10:20:30,而在手机上运行的时候,是看不到的(显示空白 : :)
详细源代码以下:


<?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="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:text="10"                      -----//android:text 改为了 tools:text
        android:textSize="25sp"
        android:minWidth="30sp"
        android:gravity="center"
        android:id="@+id/textView32" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=":"
        android:id="@+id/textView33" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:text="20" //android:text 改为了 tools:text
        android:textSize="25sp"
        android:minWidth="30sp"
        android:gravity="center"
        android:id="@+id/textView34" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=":"
        android:id="@+id/textView35" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:text="30" //android:text 改为了 tools:text
        android:textSize="25sp"
        android:minWidth="30sp"
        android:gravity="center"
        android:id="@+id/textView36" />
</LinearLayout>
相关文章
相关标签/搜索