文章目录
作用
简单的总结下就是,xmlns:android="http://schemas.android.com/apk/res/android"是xml的命名空间,比如LinearLayout容器中的控件属性都是通过xml的命名空间来辨识的。
语法定义
XML 命名空间定义语法为xmlns:namespace-prefix=“namespaceURI”,一共分为三个部分:
- xmlns: 声明命名空间的保留字,其实就是XML中元素的一个属性;
- namespace-prefix: 命名空间的前缀,这个前缀与某个命名空间相关联;
- namespaceURI: 命名空间的唯一标识符,一般就是一个URI引用。
命令空间分类
在Android中,目前我们碰到的xmlns一共有三种:
android
xmlns:android="http://schemas.android.com/apk/res/android"
- 首先我们要明白一点,xmlns是xml namespace的缩写,意思是xml命名空间。
- 那么我们就好理解这句话是什么意思了,声明这个命名空间引用的是Android系统的,而其中的android作为前缀,是这个引用别称的意思,当然我们也可以将它换成其他的名字。
- 后面schemas的意思是xml文件的约束(也就是xml的书写规范,类似于模板),还有一种xml约束是DTD,但schemas相对于DTD来说克服了DTD的局限性,扩展性强。
有了他,Android Studio就会在我们编写布局文件的时候给出提示,提示我们可以输入什么,不可以输入什么。也可以理解为语法文件,或者语法判断器。
app
xmlns:app="http://schemas.android.com/apk/res-auto"
- 在项目需求中,我们往往使用系统自带的属性以及控件是不够的,我们可能需要导入自定义控件的一些属性,或者support支持包之类的。
- 为了引入自定义的属性,我们可以xmlns:前缀=http://schemas.android.com/apk/res/你的应用程序包路径,将其导入。
- 但现在的普遍做法是使用xmlns:app=“http://schemas.android.com/apk/res-auto”,因为res-auto可以引用所有的自定义包名。
命名空间app用于我们应用自定义的一些属性,这个与 Android 自定义属性和系统控件扩展应该有关系,下面举个例子来说明一下。
tools
xmlns:tools="http://schemas.android.com/tools"
- tools顾名思义是工具的意思,可是我们使用这个命名空间能带来什么效果呐。
- tools可以告诉Android Studio,哪些属性在运行的时候是被忽略的,只在设计布局的时候有效。
- tools可以覆盖android的所有标准属性,将android:换成tools:即可;同时在运行的时候就连tools:本身都是被忽略的,不会被带进apk中。
根据官方定义,tools命名空间用于在 XML 文档记录一些,当应用打包的时候,会把这部分信息给过滤掉,不会增加应用的 size,说直白点,这些属性是为IDE提供相关信息。下面举个例子来说明一下。
tools属性的种类介绍
一种是影响Lint提示的
- tools:ignore
ignore属性是告诉Lint忽略xml中的某些警告。 - tools:targetApi
假设minSdkLevel 15,而你使用了api21中的控件比如RippleDrawable则Lint会提示警告。 - tools:locale
默认情况下res/values/strings.xml中的字符串会执行拼写检查,如果不是英语,会提示拼写错误,通过以下代码来告诉studio本地语言不是英语,就不会有提示了。
另一种是关于xml布局设计的
- tools:context
context属性其实正是的称呼是activity属性,有了这个属性,ide就知道在预览布局的时候该采用什么样的主题。同时他还可以在android studio的java代码中帮助找到相关的文件(Go to Related files) - tools:menu
告诉IDE 在预览窗口中使用哪个菜单,这个菜单将显示在layout的根节点上(actionbar的位置)。 - tools:actionBarNavMode
这个属性告诉ide app bar(Material中对actionbar的称呼)的显示模式,其值可以是:standard、tabs、list - tools:listitem/listheader/listfooter
顾名思义就是在ListView ExpandableListView等的预览效果中添加头部 尾部 以及子item的预览布局。 - tools:showIn
该属性设置于一个被其他布局的布局的根元素上。这让您可以指向包含此布局的其中一个布局,在设计时这个被包含的布局会带着周围的外部布局被渲染。这将允许您“在上下文中”查看和编辑这个布局。需要 Studio 0.5.8 或更高版本。 - tools:layout
tools:layout告诉ide,Fragment在程序预览的时候该显示成什么样
参考文章:(具体使用可参考)android中xml tools属性详解
案例详解
app
比如,在我的开源的搜素框控件中,我希望在xml布局定义的时候可以定义显示的历史记录条数,那么原生的Android输入框是无法实现我的需求,当然也可以在自定义的view中封装好函数实现“历史记录条数的设置”,但这么操作还是不够优雅,显得很臃肿,没有直接在控件布局中定义来的方便。
接下来就是,整个实现过程:
(1)设定自定义属性
在res下创建attrs.xml文件,并修改为如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SoftSearchView">
<attr name="maxHistoryLines" format="integer"/>
</declare-styleable>
</resources>
附上:自定义format的概览:
format名称 | format类型 | 用途举例 |
---|---|---|
reference | 表示引用,参考某一资源ID | 比如RelativeLayout中,需要设置控件相对位置:android:layout_toRightOf="@+id/search_src_text" |
string | 表示字符串 | 比如TextView中,需要设置默认文本内容:android:text="@string/test_text" |
color | 表示颜色值 | 比如TextView中,需要设置默认文本颜色:android:textColor="@color/green" |
boolean | 表示尺寸值 | 比如需要设置控件宽度:android:layout_width=“48dp” |
dimension | 表示布尔值 | 比如EditText中,需要设置能否获得焦点:android:focusable=“true” |
float | 表示浮点值 | 比如RatingBar,需要设置默认评分:android:rating=“1.2” |
integer | 表示整型值 | 比如EditText中,需要设置最大行数:android:maxLines=“1” |
fraction | 表示百分数 | 比如PercentRelativeLayout中,需要设置控件高度占比:app:layout_heightPercent=“80%” |
enum | 表示枚举值 | 比如TextView中,需要设置是否显示:android:visibility=“gone” |
flag | 表示位运算 | 比如TextView中,需要设置布局:android:gravity=“end|top” |
(2)布局上的修改
可以在xml中添加,这样就可以辨识出自定义控件的自定义属性
xmlns:app="http://schemas.android.com/apk/res-auto"
完整的如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.codermrye.softsearchview.SoftSearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:maxHistoryLines="3"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
(3)在自定义组件中的构造函数中获取设置的属性值
public SoftSearchView(Context context) {
super(context);
this.mContext = context;
initView(context, null);
}
public SoftSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public SoftSearchView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attributeSet) {
if (attributeSet != null) {
TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.SoftSearchView);
maxHistoryLines = a.getDimensionPixelSize(R.styleable.SoftSearchView_maxHistoryLines, maxHistoryLines);
a.recycle();//为了保持以后使用的一致性,需要回收,具体代码位置看实际项目逻辑
} else {
maxHistoryLines = 3;
}
}
tools
为了在ide中预览效果,你可以在xml中为TextView控件设置android:text属性
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" />
结语
大家好,我是程序员小小叶,欢迎关注我的公众号(程序员小小叶),如果你觉得本篇内容有帮助到你,可以转载转发但记得要关注,要标明原文哦,谢谢您的支持,您的支持是我最大的动力~