介紹
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" />
截圖效果: