Android--xmlns

xmlns:android=“http://schemas.android.com/apk/res/android” 是什么意思。这篇博客就是简单的介绍一下 Android XML 文件的命名空间是什么以及如何使用。

XML namespace

xmlns 即 XML namespace,也就是 XML 文件的命名空间,这是 XML 通用的一个特性,并非 Android 的资源文件特有。XML 命名空间使用 URI 引用来标识,是加在XML 元素和元素的属性上的约束。注意,URI 是 Identifier(标示符),所以,虽然通常会有 http:// 的字符出现,但不一定会像 URL 一样指向特定的资源。XML 的元素和元素的属性,属于不同的命名空间,就可以拥有不同的意义,甚至可以进行自定义,另外这样做也可以防止冲突。

我们可以这样定义 XML namespace:xmlns:aaa=“http://schemas.aaa.com” 这样就定义了一个“http://schemas.aaa.com” 的命名空间,它的别名是 “aaa”

下面是一个简单的同一个 XML 文件里使用多个命名空间的例子:

<?xml version="1.0"?>
<bk:book
  xmlns:bk=‘urn:loc.gov:books‘
  xmlns:isbn=‘urn:ISBN:0-395-36341-6‘>
  <bk:title>Cheaper by the Dozen</bk:title>
  <isbn:number>1568491379</isbn:number>
</bk:book>

这样 XML 的解析器就可以根据不同的命名空间来进行解析工作了。

其实,这和 Java 中类的全限定名有些类似,可以有相同的类名,只要它们处于不同的包中,就还是两个不同的类,JVM 是通过类的全限定名来加载类的。

Android 资源文件的命名空间

xmlns:android=“http://schemas.android.com/apk/res/android”

这是 Android 的 Framework 层解析 XML 布局文件默认的命名空间,android 是它的别名,所以在 View 的属性前都会有 android: 的前缀,你也可以取其它的名字,但是这个命名空间的字符串标识符是唯一、不可更改的。

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

Android 还有另外一个常用的命名空间,URI是 “http://schemas.android.com/tools”,它可以让工具提取 XML 文件的信息,并且在打包 APK 的时候去除掉此命名空间下的元素和属性,这样就不会影响运行时了。 根据 Android Tools 的官网,下面举几个常用的例子:

  • tools:ignore 这个属性可以用于任意 XML 元素,是以逗号分隔的 Lint 工具检查项目的 ID,它会忽略此元素及其子元素中相应的 Lint 检查。
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>

Used by: Lint

  • tools:targetApi 这个属性和 Java 类中的 @TargetApi 注解类似,用于指定 XML 元素运行的 API 等级,可以使用 API 等级的整数或者名称表示
<GridLayout
  tools:targetApi="ICE_CREAM_SANDWICH" >

Used by: Lint

  • tools:locale 用于资源文件的根节点,指定资源的语言或者地区。
<resources
  xmlns:tools="http://schemas.android.com/tools"
  tools:locale="es">

Used by: Lint, Studio (关闭非英语资源文件的拼写检查)

  • tools:context 通常用于 XML 布局文件的根节点,告诉工具这个布局文件是被哪个 Activity 使用(设计阶段,并非运行时)。这样做可以告诉 Layout Editor 此布局文件可以使用关联 Activity 的 theme,我们就可以看到对应 theme 下此布局实时渲染的效果。
<android.support.v7.widget.GridLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context=".MainActivity" />

Used by: Layout Editor in Studio & Eclipse, Lint

  • tools:layout 我们通常都是动态使用 Fragment 类的,其实也可以直接在布局文件中使用 fragment 标签,此时,可以使用这个属性查看 Fragment 的对应布局的渲染效果。注意,这只是作为设计阶段的辅助,运行时你可以加载其它任意布局文件。
<fragment
  android:name="com.example.master.ItemListFragment"
  tools:layout="@android:layout/list_content" />

Used by: Layout Editor in Studio & Eclipse

  • tools:listitem | listheader | listfooter 这个属性可以用于 AdapterView 的子类,包括 ListView, GridView 和 ExpandableListView 等,告诉工具在设计阶段列表渲染使用的 item 布局,header 和 footer 的布局。
<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:listitem="@android:layout/simple_list_item_2" />

Used by: Layout Editor in Studio & Eclipse

  • tools:showIn 为了方便布局的复用,我们会 include 某些公用的布局文件。在公用的布局文件的根元素中使用这个属性,可以在合适的上下文,也就是外围布局中渲染这个布局,看到全部的效果。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:text="@string/hello_world"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  tools:showIn="@layout/activity_main" />

Used by: Layout Editor in Studio

  • tools:menu 这个属性告诉工具 Action Bar 使用的菜单资源文件。
<?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="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:menu="menu1,menu2" />

Used by: Layout Editor in Studio

  • tools:actionBarNavMode 这个属性告诉工具 Action Bar 的导航模式,包括“standard”,“list”,“tabs” 三种。
<?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="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:actionBarNavMode="tabs" />

Used by: Layout Editor in Studio

Designtime Layout Attributes

既然我们上面说到的命名空间只是在设计时起作用,我们可以更进一步,覆写 Framework 提供的 View 的属性,只是在设计阶段查看界面渲染的效果,打包的时候会被去除,所以不会影响运行时。

要使用这个功能,和上面一样,首先需要在布局的根元素中加入此命名空间的声明。然后,可以把所有原来以 android: 前缀开始的 View 的属性改为 tools: 前缀。而且,这两个命名空间下的属性可以同时出现,一个是设计阶段起作用,一个是运行时起作用,互不影响。

<TextView
  tools="Dummy text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

比如上面这段 TextView 的声明,由于没有预设的文字,所以没法在工具渲染的时候看到它的样子,此时可以加入 tools=“Dummy text” ,就可以实时查看这个 TextView 的样子了,而且不会影响运行时。

再例如,有些 View 的显示与否是运行时动态控制的,这个时候我们可以这样做:

<Button
  android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="First"
  tools:visibility="invisible" />
<Button
  android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Second"
  tools:visibility="visible" />

这样我们就可以在工具渲染布局的时候动态控制 View 的可见性,十分方便。

在 Android 中自定义命名空间

上面说的都是 Android 的 Framework 和开发工具定义的 XML 命名空间,我们来看看自定义 XML 命名空间的应用。我们在 Android 的开发中,偶尔会需要自定义 View,自定义的 View 都是继承自 Framework 的 android.view.View 类或者它的子类。这个时候,如果我们想要除了 Framework 提供的属性以外的属性,就会用到自定义命名空间。

首先,我们继承 View 类,并提供接受 Context 和 AttributeSet 作为参数的构造方法,这样就可以在XML布局中直接使用自定义 View 了。

class PieChart extends View {
  public PieChart(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
}

如果我们想要自定义属性,并且可以在 XML 布局文件中直接使用,请遵守以下 4 条:

  • 在 <declare-styleable> 资源元素下定义自定义属性
  • 在 XML 布局中声明属性值
  • 在运行时获取属性值
  • 在 View 对象上应用属性值

首先,让我们来自定义属性,在 res/values/ 文件夹下添加 attrs.xml 文件,文件的内容如下:

<resources>
  <declare-styleable name="PieChart">
    <attr name="showText" format="boolean" />
    <attr name="labelPosition" format="enum">
      <enum name="left" value="0"/>
      <enum name="right" value="1"/>
    </attr>
  </declare-styleable>
</resources>

上面的代码定义了两个自定义属性, showText 和 labelPosition ,它们都属于 PieChart 的样式。样式的名称虽然不一定非要和自定义 View 的类名相同,但是有些代码编辑器是通过名称来进行关联的,所以最好是遵循这个习惯。

下面就可以在 XML 布局文件中使用自定义属性了,但是和 Framework 提供的内置属性不同的是,自定义属性不是属于 http://schemas.android.com/apk/res/android 命名空间的,它是属于http://schemas.android.com/apk/res/[your-package-name] 命名空间的。下面就是使用自定义属性的示例代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
  <com.example.customviews.charting.PieChart
    custom:showText="true"
    custom:labelPosition="left" />
</LinearLayout>

上面我们定义了 custom 的命名空间别名,当然你也可以使用其它的别名。

当 XML 布局中的 View 对象被创建的时候,它的属性会被封装进 AttributeSet 对象传进此 View 的构造方法。虽然我们可以直接从 AttributeSet 对象读取属性值,但是更好的方法是将 AttributeSet 对象传给 obtainStyledAttributes() 方法,这个方法会返回一个包含了属性数组的 TypedArray 对象。下面就是 PieChart 类如何读取属性的:

public PieChart(Context context, AttributeSet attrs) {
  super(context, attrs);
  TypedArray a = context.getTheme().
          obtainStyledAttributes(attrs, R.styleable.PieChart, 0, 0);
  try {
    mShowText=a.getBoolean(R.styleable.PieChart_showText,false);
    mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);
  } finally {
    a.recycle();
  }
}

属性可以控制 View 的样子,但是只有 View 初始化以后才可以读取它的属性。因此,为了提供动态的行为,需要为每一个自定义属性提供 Getter 和 Setter 方法。下面就是 PieChart 的 showText 相关的代码片段:

public boolean isShowText() {
  return mShowText;
}

public void setShowText(boolean showText) {
  mShowText = showText;
  invalidate();
  requestLayout();
}

注意,为了保证 View 处于正确的状态,上面的代码调用了 invalidate() 和 requestLayout() 两个方法。这样,你就可以在自定义的 View 类中根据自定义的属性控制 View 的行为了。

时间: 2024-08-25 15:36:30

Android--xmlns的相关文章

android xmlns:tools用法

Android开发中在布局文件里面都会有如下面的内容: <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent"

android中xmlns:tools属性详解

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

xmlns:android=&quot;http://schemas.android.com/apk/res/android的作用是

xmlns:android="http://schemas.android.com/apk/res/android的作用是 这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为语法文件.或者语法判断器什么的 这个主要作用是在运行的时候那些控件的属性都是通过它来识别的,如果上面你写错了,不会有任何问题,但是在运行的时候就会有问题,提示你没有指定宽度等什么.这个是不用联网的. Android 自定义的xmlns其实很简单,语法

android 布局文件中xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;

http://blog.163.com/benben_long/blog/static/199458243201411394624170/ xmlns:android="http://schemas.android.com/apk/res/android的作用是 这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为语法文件.或者语法判断器什么的 这个主要作用是在运行的时候那些控件的属性都是通过它来识别的,如果上面你写错了,

Android中如何使用xmlns

http://blog.csdn.net/lihenair/article/details/41009711 工作中时常需要自定义控件,除了按键,draw以外,还需要对控件属性进行一些初始化的操作,比如控件的边距,字体大小,颜色等. 本文将根据需求,实现一个自定义的TextView. 1 需求 要求TextView的字体是30sp,颜色是#FF00FFAD. 针对这两个需求,做出如下定义 colors.xml [html] view plain copy <?xml version="1.

xmlns:android的说明

xmlns:android xmlns:android:命名空间的声明 "xmlns:android这是一个XML命名空间,告诉Android开发工具你准备使用Android命名空间里的一些通用属性. 在所有Android XML设计文件中最外层的标记必须使用这个树形. xmlns:android="http://schemas.android.com/apk/res/android".这样使得Android中各种标准属性能在文件中使用,提供了大部分元素中的数据. 有了它,你

Android小例子:使用反射机制来读取图片制作一个图片浏览器

效果图: 工程文件夹: 该例子可供于新手参考练习,如果有哪里不对的地方,望指正>-< <黑幕下的人> java代码(MainActivity.java): package com.example.imageswitchtest; import java.lang.reflect.Field; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.v

Android 导航条效果实现(六) TabLayout+ViewPager+Fragment

TabLayout 一.继承结构 public class TabLayout extends HorizontalScrollView java.lang.Object ? android.view.View ? android.view.ViewGroup ? android.widget.FrameLayout ? android.widget.HorizontalScrollView ? android.support.design.widget.TabLayout 二.TabLayou

谈谈-Android中的接口回调技术

Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James对Android接口回调技术的理解(结合前人的知识和自己的实践). 使用一个比喻很形象地说明:客户端有个疑问打电话请教服务端,但服务端无法现场给出解答,相互之间约定:服务端一旦有答案,使用电话的方式反馈给客户端. 以上有三个主体:客户端.服务端和接口(方式). 接口回调的原理框图说明: Demo界面

推荐android布局百分比框架

githup:https://github.com/JulienGenoud/android-percent-support-lib-sample 下面是使用方法: Android Percent Support Lib Sample  I made a sample of the new percent support library.You can check official docs reference here and here.This library provide percent