当前位置:   article > 正文

android 布局xml文件中的 tools 属性_as xml中tools

as xml中tools




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

android:id=

android:layout_marginLeft=

android:text=

...

等属性

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

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函数来动态设置更新获取到的时间。

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

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>



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/943996
推荐阅读
相关标签
  

闽ICP备14008679号