《Android 群英传》——徐宜生 读书笔记
Android 控件架构
通常,在Activity中使用setContentView() 方法来设置一个布局,在调用该方法后,布局内容才真正显示出来。首先我们来看一下Android界面的架构图:每个Activity都包含一个Window对象,在Android中Window对象通常由PhoneWindow来实现,PhoneWindow将一个DecorView作为整个应用窗口的根View,即窗口界面的顶级视图。DecorView将整个界面分为两部分,顶部的ActionBar和主体的ContentView,而setContentView()方法就是为ContentView部分加载布局的。并且,我们平时编写的界面基本都是在ContentView中定义的。下面我们可以来验证一下:
LinearLayout linearLayout = findViewById(R.id.linear_layout);
ViewGroup content = (ViewGroup) linearLayout.getParent();
Log.d("jh", content.toString());
ViewGroup viewGroup = (ViewGroup)content.getParent();
Log.d("jh", viewGroup.toString());
ViewGroup decorView = (ViewGroup)viewGroup.getParent();
Log.d("jh", decorView.toString());
-
先打印出来看一下:
这样还不够清晰,我们再来从DecorView向下打印来看看:
Log.d("jh", decorView.toString());
Log.d("jh", String.valueOf(decorView.getChildCount()));
Log.d("jh", viewGroup.toString());
Log.d("jh", String.valueOf( viewGroup.getChildCount()));
Log.d("jh", viewGroup.getChildAt(0).toString());
Log.d("jh", viewGroup.getChildAt(1).toString());
Log.d("jh", String.valueOf(content.getChildCount()));
Log.d("jh", content.getChildAt(0).toString());
可以看到,DecorView实际上是一个FrameLayout,它有一个子View,是一个FitWindowsLinearLayout,这个LinearLayout有两个子View,也就是我们的 ActionBar(加了一个ViewStub嵌套,因为ActionBar可以设置显示与否) 和 ContentView(id 是 android.R.id.content ,实际也是一个FrameLayout),而ContentView 的子布局就是我们平时编写界面的最外层布局,也就是说,其实setContentView方法就是将我们的 activity_main.xml 动态加载到这个ContentView里面的。虽然绕了一大圈,但是我们对我们所能看到的UI界面的架构也了解的清晰了很多。
View的测量
Android在绘制一个View前,必须先对View进行测量,确定View的宽高,这个过程在onMeasure()方法中进行。Android系统提供了一个MeasureSpec类,来帮助我们测量View。MeasureSpec定义了一个32位的Int值,其中高2位表示测量模式,低30位表示测量值的大小。
-
测量模式有三种:
- EXACTLY:即精确值模式,当我们指定控件的 layout_width 或 layout_height 属性为具体数值或match_parent(实际也是准确值)时,系统会使用 EXACTLY 模式。
- AT_MOST:即最大值模式,当控件的 layout_width 或 layout_height 指定为wrap_content 时,View的大小是根据View的内容宽高来确定的,并且受制于父View的宽高,这时系统会使用AT_MOST模式。
- UNSPECIFIED:这种模式下,不限定View的宽高,想多大就多大,一般在自定义View时使用。
View类默认的onMeasure()方法只支持EXACTLY模式,因此,当我们自定义View时,如果想为控件指定wrap_content属性,就必须重写onMeasure()方法,来对其进行处理。下面,我们来绘制一个简单的矩形,来观察一下这三种测量模式。
-
简单实例
- 首先我们要新建一个RectangleView类,继承View类。
public class RectangleView extends View { public RectangleView(Context context, AttributeSet attr) { super(context, attr); } }
- 我们查看View类的 onMeasure() 方法,会发现它其实是调用了setMeasuredDimension() 方法将测量所得的宽高传递进去,从而完成测量工作。因此我们需要做的就是对这个方法传递的参数进行处理,从而定制我们的矩形宽高。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); }
onMeasure()方法的两个参数就是系统确定的当前View的宽高。
- 我们定义一个measureWidth() 和 一个measureHeight() 方法,用来控制View的宽高(下面以measureWidth为例)。
private int measureWidth(int measureSpec) { int result; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; }else { result = 200; if (specMode == MeasureSpec.AT_MOST){ result = Math.min(result, specSize); } } return result; }
这里传入的参数就是onMeasure()方法指定的宽高,我们首先从这个MeasureSpec对象中提取出具体的测量模式和大小。如果测量模式是EXACTLY模式,就表明这里的宽高就是我们通过layout_width属性指定的精确宽高,那么就直接返回这个值;如果测量模式是另外两种,我们可以给这个View指定一个默认的大小(单位是px),当测量模式是UNSPECIFIED模式,矩形的宽就是这个默认值,如果是AT_MOST模式(也就意味着我们指定的layout_width是wrap_content),我们还需要取我们指定的默认大小和系统确定的宽度(一般就是父View的宽)中的较小值,因为这种模式下,View的宽高不能大于父View的宽高。最后就是将处理好的宽度值返回。
- 在onMeasure()方法中调用 setMeasuredDimension() 方法。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); }
下面来看看我们自定义的RectangleView是什么效果:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <com.laughter.view.views.RectangleView android:layout_width="300px" android:layout_height="300px" android:background="@color/colorPrimary"/> <com.laughter.view.views.RectangleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@color/colorPrimary"/> <com.laughter.view.views.RectangleView android:layout_width="200px" android:layout_height="300px" android:layout_marginTop="20dp" android:background="@color/colorPrimary"/> </LinearLayout>
可以看出,当我们为RectngleView指定具体宽高时,宽高就是指定值;当我们指定为wrap_content时,宽高就是默认大小(200px),当然,如果父View的宽高小于200px,这里的宽高就会适应父View(不能大于父View的宽高)。还有一点需要注意:前面说过,View类的onMeasure方法只支持EXACTLY模式,因此,如果我们不重写onMeasure()方法,也就意味着不处理AT_MOST模式,当我们指定wrap_content时,View的宽高会匹配父View(即和match_parent效果相同)。感兴趣可以自行测试。
关于View绘制的部分,由于书上没有详细讲到关于Canvas的内容,这里推荐一个系列教程,不过建议先理解清楚上面的内容,再去看的话会很轻松:
Android自定义View系列教程
这个系列博客内容十分详实,由浅入深,很适合初学者学习。