Android 自定义view中的属性,命名空间,以及tools标签

时间:2022-07-02 13:03:46

昨日看到有人在知乎上问这3个琐碎的小知识点,今天索性就整理了一下,其实这些知识点并不难,但是很多开发者平时很少注意到这些,

导致的后果就是开发的时候 经常会被ide报错,开发效率很低,或者看开源代码的时候很多地方看不懂。

考虑到现在越来越多的人开发环境迁移到android studio,所以一切以android studio环境为准。和eclipse开发环境相比其实两者是差不多的,

偶有区别 主要也是android studio引入的gradle脚本造成差异。

首先来看看tools标签。

Android 自定义view中的属性,命名空间,以及tools标签

这个地方很多人不明白xmlns:tools  这行代码是干嘛的,好像删除了以后对程序也没么影响,实际上这个tools标签主要是为adt插件使用的。

他里面的很多属性能在很大程度上方便我们的开发,但是并不会影响我们最终生成的apk包。比如大家在写一个界面的时候一般都会给

textview写上text的值,然后在开发完毕的时候再删除他,这个操作就很麻烦,但是现在你就可以。

Android 自定义view中的属性,命名空间,以及tools标签

如果加上tools:text 你就可以在界面预览中看到效果,但是实际运行时是不会有效果的。很方便的,同样的以往我们在开发listview的时候之所以累就是无法预览listview的item效果,

每次都得运行以后才能看到。但是现在你只需要利用tools标签。

Android 自定义view中的属性,命名空间,以及tools标签

然后你无需run你的程序 直接在界面预览就能看到item的效果

Android 自定义view中的属性,命名空间,以及tools标签

官方给出的文档在这里 http://tools.android.com/tech-docs/tools-attributes

有兴趣的同学可以上去自己看看,试试看这些标签,对开发速度会有显著的提升的~~

另外再说下 res和res-auto的区别。

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

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

这2个实际上前者是就是让你引用系统自带属性的,后者是让你使用lib库里自定义属性的。

但是这个地方要注意,在eclipse中如果要使用你自定义的属性 是不能用res-auto的

必须得替换成你自定义view所属的包名,如果你在恰好使用的自定义属性被做成了lib

那就只能使用res-auto了,而在android-studio里,无论你是自己写自定义view

还是引用的lib里的自定义的view 都只能使用res-auto这个写法。以前那个包名的写法

在android-studio里是被废弃无法使用的。

最后我们来看看TypedArray和attrs之间的区别异同以及在自定义view里的应用。

首先我们自定义几个属性

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="attrName">
<attr name="name" format="string"></attr>
<attr name="number" format="integer"></attr>
</declare-styleable> </resources>

然后布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <com.example.administrator.popupmenu.CustomView
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="@dimen/padding"
customview:name="@string/hello_world"
customview:number="123" /> </LinearLayout>

然后看下自定义view的源码

 package com.example.administrator.popupmenu;

 import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View; /**
* Created by Administrator on 2015/8/18.
*/
public class CustomView extends View { private static final String TAG = CustomView.class.getSimpleName(); public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.attrName);
String name = ta.getString(R.styleable.attrName_name);
int number = ta.getInteger(R.styleable.attrName_number, -1);
Log.e(TAG, "name=" + name + " number=" + number); /**
* attrs在取值的时候 缺陷就是如果值里面还有类似的引用 则取不到正确的值
* 需要额外
*
*/
for (int i = 0; i < attrs.getAttributeCount(); i++) {
Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + attrs.getAttributeValue(i));
//取出來實際的像素的值
if (attrs.getAttributeName(i).equals("padding")) {
;
Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + getResources().getDimension(attrs.getAttributeResourceValue(i, -1))); }
//这个地方就能看出来TypedArray比attrs要好用的多~同时也可以理解两者区别了
if (attrs.getAttributeName(i).equals("name")) {
;
Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + getResources().getString(attrs.getAttributeResourceValue(i, -1))); }
}
ta.recycle(); }
}

最后看下我们的输出。

Android 自定义view中的属性,命名空间,以及tools标签