0%

Android 自定义属性

1.定义自定义属性, values下创建attrs.xml, 或者可根据模块创建attr_***.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="***" format="***" /> //定义属性
<attr name="test_color" format="color"/> //定义一个属性名称为test_color, 接收color的类型
<attr name="test_text" format="string"/> //定义一个属性名称为test_text, 接受String类型

<declare-styleable name="test_attr"> //算是定义一个属性的集合, 包含多条自定义属性
<attr name="test_text"/>
</declare-styleable>

</resources>
Read more »

一.@代表引用资源

1.引用自定义资源。格式:@[package:]type/name

1
android:text="@string/hello"

2.引用系统资源。格式:@android:type/name

1
android:textColor="@android:color/opaque_red"
Read more »

四个构造函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class CustomView extends View {
//1
public CustomView(Context context) {
super(context);
}
//2:
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
//3
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//4
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
Read more »

在布局优化中,Androi的官方提到了这三种布局 include、merge、ViewStub,并介绍了这三种布局各有的优势,下面也是简单说一下他们的优势,以及怎么使用,记下来权当做笔记。

Read more »