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

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

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

考虑到如今愈来愈多的人开发环境迁移到android studio,因此一切以android studio环境为准。和eclipse开发环境相比其实二者是差很少的,eclipse

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

首先来看看tools标签。布局

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

他里面的不少属性能在很大程度上方便咱们的开发,可是并不会影响咱们最终生成的apk包。好比你们在写一个界面的时候通常都会给gradle

textview写上text的值,而后在开发完毕的时候再删除他,这个操做就很麻烦,可是如今你就能够。spa

若是加上tools:text 你就能够在界面预览中看到效果,可是实际运行时是不会有效果的。很方便的,一样的以往咱们在开发listview的时候之因此累就是没法预览listview的item效果,插件

每次都得运行之后才能看到。可是如今你只须要利用tools标签。code

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

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

有兴趣的同窗能够上去本身看看,试试看这些标签,对开发速度会有显著的提高的~~

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

1 xmlns:android="http://schemas.android.com/apk/res/android"
2 
3 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里的应用。

首先咱们自定义几个属性

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <declare-styleable name="attrName">
4         <attr name="name" format="string"></attr>
5         <attr name="number" format="integer"></attr>
6     </declare-styleable>
7 
8 
9 </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的源码

 1 package com.example.administrator.popupmenu;
 2 
 3 import android.content.Context;
 4 import android.content.res.TypedArray;
 5 import android.util.AttributeSet;
 6 import android.util.Log;
 7 import android.view.View;
 8 
 9 /**
10  * Created by Administrator on 2015/8/18.
11  */
12 public class CustomView extends View {
13 
14     private static final String TAG = CustomView.class.getSimpleName();
15 
16     public CustomView(Context context, AttributeSet attrs) {
17         super(context, attrs);
18         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.attrName);
19         String name = ta.getString(R.styleable.attrName_name);
20         int number = ta.getInteger(R.styleable.attrName_number, -1);
21         Log.e(TAG, "name=" + name + " number=" + number);
22 
23         /**
24          * attrs在取值的时候 缺陷就是若是值里面还有相似的引用 则取不到正确的值
25          * 须要额外
26          *
27          */
28         for (int i = 0; i < attrs.getAttributeCount(); i++) {
29             Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + attrs.getAttributeValue(i));
30             //取出來實際的像素的值
31             if (attrs.getAttributeName(i).equals("padding")) {
32                 ;
33                 Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + getResources().getDimension(attrs.getAttributeResourceValue(i, -1)));
34 
35             }
36             //这个地方就能看出来TypedArray比attrs要好用的多~同时也能够理解二者区别了
37             if (attrs.getAttributeName(i).equals("name")) {
38                 ;
39                 Log.e(TAG, "attrs name=" + attrs.getAttributeName(i) + " attrs value=" + getResources().getString(attrs.getAttributeResourceValue(i, -1)));
40 
41             }
42         }
43         ta.recycle();
44 
45 
46     }
47 }

 

最后看下咱们的输出。

相关文章
相关标签/搜索