Attributes, namespaces, and tools tags in Android custom views

Original: http://www.w2bc.com/Article/59242

Yesterday, I saw someone asking about these 3 trivial knowledge points on Zhihu. Today, I simply sorted them out. In fact, these knowledge points are not difficult, but many developers rarely notice them.

The consequence is that errors are often reported by IDE during development, the development efficiency is very low, or many places cannot be understood when looking at open source code.

Considering that more and more people's development environment has been migrated to android studio, everything is subject to the android studio environment. Compared with the eclipse development environment, the two are actually similar.

The occasional difference is mainly caused by the gradle script introduced by android studio.

First look at the tools tab.

Many people in this place do not understand what the xmlns:tools line of code is for. It seems that it will not affect the program after deleting it. In fact, the tools tag is mainly used for the adt plugin.

Many properties in it can greatly facilitate our development, but it will not affect the final apk package we generate. For example, when people write an interface, they usually give

The textview writes the value of text, and then deletes it when the development is complete. This operation is very troublesome, but now you can.

If you add tools:text, you can see the effect in the interface preview, but it will not have effect when it is actually running. It's very convenient. In the past, the reason why we were tired when developing listview was that we couldn't preview the item effect of listview.

You have to run it every time to see it. But now you just need to make use of the tools tag.

Then you can see the effect of the item directly in the interface preview without running your program

The official documentation is here http://tools.android.com/tech-docs/tools-attributes 

Interested students can go up and see for themselves, try these tags, and the development speed will be significantly improved~~

In addition, let's talk about the difference between res and 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 }

 

最后看下我们的输出。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326609503&siteId=291194637