Android开发实践:为什么要继承onMeasure()
来源:互联网 发布:一对一网络课堂软件 编辑:程序博客网 时间:2024/05/24 07:33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package
com.titcktick.customview;
import
android.content.Context;
import
android.util.AttributeSet;
import
android.view.View;
public
class
CustomView
extends
View {
public
CustomView(Context context) {
super
(context);
}
public
CustomView(Context context, AttributeSet attrs) {
super
(context, attrs);
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
super
.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
>
<com.titcktick.customview.CustomView
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:layout_margin=
"10dp"
android:background=
"@android:color/black"
/>
</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package
com.titcktick.customview;
import
android.content.Context;
import
android.util.AttributeSet;
import
android.view.View;
public
class
CustomView
extends
View {
private
static
final
int
DEFAULT_VIEW_WIDTH =
100
;
private
static
final
int
DEFAULT_VIEW_HEIGHT =
100
;
public
CustomView(Context context) {
super
(context);
}
public
CustomView(Context context, AttributeSet attrs) {
super
(context, attrs);
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
int
width = measureDimension(DEFAULT_VIEW_WIDTH, widthMeasureSpec);
int
height = measureDimension(DEFAULT_VIEW_HEIGHT, heightMeasureSpec);
setMeasuredDimension(width, height);
}
protected
int
measureDimension(
int
defaultSize,
int
measureSpec ) {
int
result = defaultSize;
int
specMode = MeasureSpec.getMode(measureSpec);
int
specSize = MeasureSpec.getSize(measureSpec);
//1. layout给出了确定的值,比如:100dp
//2. layout使用的是match_parent,但父控件的size已经可以确定了,比如设置的是具体的值或者match_parent
if
(specMode == MeasureSpec.EXACTLY) {
result = specSize;
//建议:result直接使用确定值
}
//1. layout使用的是wrap_content
//2. layout使用的是match_parent,但父控件使用的是确定的值或者wrap_content
else
if
(specMode == MeasureSpec.AT_MOST) {
result = Math.min(defaultSize, specSize);
//建议:result不能大于specSize
}
//UNSPECIFIED,没有任何限制,所以可以设置任何大小
//多半出现在自定义的父控件的情况下,期望由自控件自行决定大小
else
{
result = defaultSize;
}
return
result;
}
}
0 0
- Android开发实践:为什么要继承onMeasure()
- Android开发实践:为什么要继承onMeasure()
- Android_为什么要继承onMeasure()
- 为什么要使用onMeasure
- 为什么你要重写View的onMeasure()?
- Android自定义组件开发之onMeasure使用
- Android自定义组件开发之onMeasure使用
- Android 继承ViewGroup必须重写onMeasure方法和onLayout方法
- Android自定义View-------为什么重写onMeasure()以及怎么重写
- Android自定义View-------为什么重写onMeasure()以及怎么重写
- android Onmeasure
- Android OnMeasure
- Android onMeasure()
- 为什么要学习Android开发?
- 为什么要继承
- 为什么要继承ActionSupport
- Android开发之自定义控件(一)---onMeasure详解
- Android开发之自定义控件(一)---onMeasure详解
- 43个优秀的Swift开源项目推荐
- form builder视图窗口下找不到堆叠视图项
- Python基础教程(一):简介、环境搭建、中文编码
- Paul Graham文集
- poj2449 Remmarguts' Date A*K短路
- Android开发实践:为什么要继承onMeasure()
- hibernate底层dao
- Linux磁盘分区
- 设计模式系列(二)观察者模式(Observer Pattern)
- linux下动态域名解析
- 项目总结、、
- mongodb创建索引
- 对递归执行过程的简单描述
- 商城综合版优化札记-优惠券