对于自定义属性,你们确定都不陌生,遵循如下几步,就能够实现:javascript
ps:若是你对上述几个步骤不熟悉,建议先熟悉下,再继续~
那么,我有几个问题: java
恩,针对这几个问题,你们能够考虑下,如何回答呢?仍是说:老子会背上述4个步骤就够了~~android
接下来经过例子来回答上述问题,问题的回答顺序不定~~你们先看一个常见的例子,即上述几个步骤的代码化。 面试
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="test">
<attr name="text" format="string" />
<attr name="testAttr" format="integer" />
</declare-styleable>
</resources>
复制代码
package com.example.test;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class MyTextView extends View {
private static final String TAG = MyTextView.class.getSimpleName();
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);
String text = ta.getString(R.styleable.test_testAttr);
int textAttr = ta.getInteger(R.styleable.test_text, -1);
Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);
ta.recycle();
}
}
复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:zhy="http://schemas.android.com/apk/res/com.example.test"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.test.MyTextView android:layout_width="100dp" android:layout_height="200dp" zhy:testAttr="520" zhy:text="helloworld" /> </RelativeLayout>
复制代码
ok,你们花3s扫一下,运行结果为: 数组
MyTextView: text = helloworld , textAttr = 520 复制代码
应该都不意外吧,注意下,个人styleable的name写的是test,因此说这里并不要求必定是自定义View的名字。 性能优化
下面考虑: bash
构造方法中的有个参数叫作AttributeSet(eg: MyTextView(Context context, AttributeSet attrs) )这个参数看名字就知道包含的是参数的集合,那么我能不能经过它去获取个人自定义属性呢?
首先AttributeSet中的确保存的是该View声明的全部的属性,而且外面的确能够经过它去获取(自定义的)属性,怎么作呢? 架构
其实看下AttributeSet的方法就明白了,下面看代码。布局
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
int count = attrs.getAttributeCount();
for (int i = 0; i < count; i++) {
String attrName = attrs.getAttributeName(i);
String attrVal = attrs.getAttributeValue(i);
Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);
}
// ==>use typedarray ...
}
复制代码
输出: 性能
MyTextView(4136): attrName = layout_width , attrVal = 100.0dip
MyTextView(4136): attrName = layout_height , attrVal = 200.0dip
MyTextView(4136): attrName = text , attrVal = helloworld
MyTextView(4136): attrName = testAttr , attrVal = 520 复制代码
结合上面的布局文件,你发现了什么?
我擦,果真很神奇,真的得到全部的属性,恩,没错,经过AttributeSet能够得到布局文件中定义的全部属性的key和value(还有一些方法,本身去尝试),那么是否是说TypedArray这个鬼能够抛弃了呢?答案是:NO!。
如今关注下一个问题:
TypedArray是什么鬼?从哪冒出来的,就要我去使用?
咱们简单修改下,布局文件中的MyTextView的属性。
<com.example.test.MyTextView
android:layout_width="@dimen/dp100"
android:layout_height="@dimen/dp200"
zhy:testAttr="520"
zhy:text="@string/hello_world" /> 复制代码
如今再次运行的结果是:
MyTextView(4692): attrName = layout_width , attrVal = @2131165234
MyTextView(4692): attrName = layout_height , attrVal = @2131165235
MyTextView(4692): attrName = text , attrVal = @2131361809
MyTextView(4692): attrName = testAttr , attrVal = 520
>>use typedarray
MyTextView(4692): text = Hello world! , textAttr = 520 复制代码
发现了什么?经过AttributeSet获取的值,若是是引用都变成了@+数字的字符串。你说,这玩意你能看懂么?那么你看看最后一行使用TypedArray获取的值,是否是瞬间明白了什么。
TypedArray实际上是用来简化咱们的工做的,好比上例,若是布局中的属性的值是引用类型(好比:@dimen/dp100),若是使用AttributeSet去得到最终的像素值,那么须要第一步拿到id,第二步再去解析id。而TypedArray正是帮咱们简化了这个过程。
贴一下:若是经过AttributeSet获取最终的像素值的过程:
int widthDimensionId = attrs.getAttributeResourceValue(0, -1);
Log.e(TAG, "layout_width= "+getResources().getDimension(widthDimensionId)); 复制代码
ok,如今别人问你TypedArray存在的意义,你就能够告诉他了。
咱们已经解决了两个问题,接下来,咱们看看布局文件,咱们有一个属性叫作:zhy:text。 总所周知,系统提供了一个属性叫作:android:text,那么我以为直接使用android:text更nice,这样的话,考虑问题:
若是系统中已经有了语义比较明确的属性,我能够直接使用嘛?
答案是能够的,怎么作呢?
直接在attrs.xml中使用android:text属性。
<declare-styleable name="test">
<attr name="android:text" />
<attr name="testAttr" format="integer" />
</declare-styleable>复制代码
注意,这里咱们是使用已经定义好的属性,不须要去添加format属性(注意声明和使用的区别,差异就是有没有format)。
而后在类中这么获取:ta.getString(R.styleable.test_android_text);布局文件中直接android:text="@string/hello_world"便可。
这里提一下,系统中定义的属性,其实和咱们自定义属性的方式相似,你能够在sdk/platforms/android-xx/data/res/values该目录下看到系统中定义的属性。而后你能够在系统提供的View(eg:TextView)的构造方法中发现TypedArray获取属性的代码(本身去看一下)。
ok,接下来,我在想,既然declare-styleable这个标签的name都能随便写,这么随意的话,那么考虑问题:
styleable 的含义是什么?能够不写嘛?我自定义属性,我声明属性就行了,为何必定要写个styleable呢?
其实的确是能够不写的,怎么作呢?
那么如今的attrs.xml为:
<?xml version="1.0" encoding="utf-8"?>
<resources> <attr name="testAttr" format="integer" /> </resources> 复制代码
哟西,so清爽~
* MyTextView实现
package com.example.test;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class MyTextView extends View {
private static final String TAG = MyTextView.class.getSimpleName();
private static final int[] mAttr = { android.R.attr.text, R.attr.testAttr };
private static final int ATTR_ANDROID_TEXT = 0;
private static final int ATTR_TESTATTR = 1;
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// ==>use typedarray
TypedArray ta = context.obtainStyledAttributes(attrs, mAttr);
String text = ta.getString(ATTR_ANDROID_TEXT);
int textAttr = ta.getInteger(ATTR_TESTATTR, -1);
//输出 text = Hello world! , textAttr = 520
Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);
ta.recycle();
}
} 复制代码
貌似多了些代码,能够看到咱们声明了一个int数组,数组中的元素就是咱们想要获取的attr的id。而且咱们根据元素的在数组中的位置,定义了一些整形的常量表明其下标,而后经过TypedArray进行获取。 能够看到,咱们本来的:
R.styleable.test => mAttr
R.styleable.test_text => ATTR_ANDROID_TEXT(0)
R.styleable.test_testAttr => ATTR_TESTATTR(1) 复制代码
那么其实呢?android在其内部也会这么作,按照传统的写法,它会在R.java生成以下代码:
public static final class attr {
public static final int testAttr=0x7f0100a9;
}
public static final class styleable {
public static final int test_android_text = 0;
public static final int test_testAttr = 1;
public static final int[] test = {
0x0101014f, 0x7f0100a9
};
} 复制代码
ok,根据上述你应该发现了什么。styleale的出现系统能够为咱们完成不少常量(int[]数组,下标常量)等的编写,简化咱们的开发工做(想一想若是一堆属性,本身编写常量,你得写成什么样的代码)。那么你们确定还知道declare-styleable的name属性,通常状况下写的都是咱们自定义View的类名。主要为了直观的表达,该declare-styleable的属性,都是改View所用的。
ok,如今5个问题,回答了4个,第一个问题:
自定义属性的几个步骤是如何奏效的?
恩,上述以及基本涵盖了这个问题的答案,你们本身总结,因此:略。
总结下今天的博客。
最后送福利了,如今关注我而且加入群聊能够获取包含源码解析,自定义View,动画实现,架构分享等。 内容难度适中,篇幅精炼,天天只需花上十几分钟阅读便可。 你们能够跟我一块儿探讨,欢迎加群探讨,有flutter—性能优化—移动架构—资深UI工程师 —NDK相关专业人员和视频教学资料,还有更多面试题等你来拿~ 群号:661841852
点击连接加入群聊【Android开发行业交流】:https://jq.qq.com/?_wv=1027&k=5E5tkJ5