介绍
Space常用于布局中组件之间的间隔,draw(Canvas canvas)方法为空,没有进行view绘制,所以space提高了绘制效率,正因为draw(Canvas canvas)实现为空,所以Space不能设置背景色
源码
public final class Space extends View {
public Space(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
public Space(Context context, AttributeSet attrs, int defStyleAttr) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
public Space(Context context, AttributeSet attrs) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
public Space(Context context) {
super((Context)null, (AttributeSet)null, 0, 0);
throw new RuntimeException("Stub!");
}
public void draw(Canvas canvas) {
throw new RuntimeException("Stub!");
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
throw new RuntimeException("Stub!");
}
}
使用
android.support.v7.widget.Space
android.support.v4.widget.Space
Space
v7包的已经废弃,可以直接使用Space,也可以导入v4包,如果已经导入v7或v13包,不用再导入v4,因为v7,v13包中包含v4
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="10dp" />
截图效果: