1.越简单越好
如果一个窗口包含很多视图
[1] 启动动时间长
[2] 测量时间长
[3] 布局时间长
[4] 绘制时间长
如果视图树深度太深
[1] StackOverflowException
[2] 用户界面反应速度很慢
2.解决的办法
[1] 使用TextView的复合drawables减少层次
[2] 使用ViewStub延迟展开视图
[3] 使用<merge>合并中间视图
[4] 使用RelativeLayout减少层次
[5] 使用自定义视图
[6] 使用自定义布局
关于[1]
替代为
关于[2] 使用ViewStub延迟展开视图
首先在XML布局文件中定义 ViewStub
<ViewStub android:id = "@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
在需要展开视图时
findViewById(R.id.stub_import).setVisibility(View.VISIBLE);
// 或者
View importPanel = ((ViewStub)
findViewById(R.id.stub_import)).inflate();
展开之后变成
关于[3]使用<merge>合并视图:略
关于[4]使用RelativeLayout减少层次,总得意思就是用RelativeLayout解决复杂的布局,比如代替LinearLayout复杂的布局
关于[5]自定义视图
class CustomView extends View {
@Override
protected void onDraw(Canvas canvas) {
// 加入你的绘图编码
}
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
// 计算视图的尺寸
setMeasuredDimension(widthSpecSize, heightSpecSize);
}
}
关于[6]自定义布局
class GridLayout extends ViewGroup {
@Override
protected void onLayout(boolean changed, int l, int t,
int r, int b) {
final int count = getChildCount();
for (int i=0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
// 计算子视图的位置
child.layout(left, top, right, bottom);
}
}
}
}