[Android]Space控件的應用場景

Space控件是在Android 4.0中加入,是個空白的view,一般用於填充View組件中的間隙。

support-v4包裏提供了兼容低版本的Space控件。

源碼分析

Space控件源碼非常簡單,先來看看

public class Space extends View {

    public Space(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (getVisibility() == VISIBLE) {
            setVisibility(INVISIBLE);
        }
    }

    public Space(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Space(Context context) {
        this(context, null);
    }

    /**
     * Draw nothing.
     *
     * @param canvas an unused parameter.
     */
    @Override
    public void draw(Canvas canvas) {
    }

    /**
     * Compare to: {@link View#getDefaultSize(int, int)}
     * If mode is AT_MOST, return the child size instead of the parent size
     * (unless it is too big).
     */
    private static int getDefaultSize2(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = size;
                break;
            case MeasureSpec.AT_MOST:
                result = Math.min(size, specSize);
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(
                getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec));
    }
}

該控件直接繼承View組件,基本每個View控件都有onDraw方法用來繪製自身,而Space控件onDraw方法進行了一個空的實現。

Space控件在佈局中只佔位置,而不去繪製渲染。
組件中的間隙用Space控件填充比用其它控件填充可以提高繪製效率。

應用場景

下面是UI提供的兩張效果圖,圖一是沒有軟鍵盤的效果,圖二是有軟鍵盤的效果。

圖一
圖二

需要注意的是,當鍵盤彈出的時候,並沒有把上面的toolbar擠掉。而是壓縮了原有的佈局。
這時候我們需要讓activity配置windowSoftInputMode爲adjustResize,而不是使用默認值

 <activity
         android:name="..."
         android:windowSoftInputMode="adjustResize" />

中間的佈局並沒有完全居中,而是居中偏上。直接定義相對父容器居中不太理想, marginTop之類的又不太容易適配。
所以我採取了比較容易適配的方式。
這裏寫圖片描述

我把中間佈局上下兩端用Space填充,又通過weight控制,當鍵盤彈出的時候會自動壓縮Space空間,這樣適配就非常簡單了。

佈局代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.troila.tdv.ui.SettingIPActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    <android.support.v4.widget.Space
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="4"/>
    <LinearLayout
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/setting_bg">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_marginBottom="@dimen/login_bottom_15"
            android:layout_marginTop="@dimen/login_top_15"
            android:textStyle="bold"
            android:textSize="@dimen/text_large"
            android:textColor="@color/white"
            android:text="配置服務器信息"/>
        <include layout="@layout/divider"/>
        <LinearLayout
            android:paddingTop="@dimen/login_top_20"
            android:paddingBottom="@dimen/login_bottom_15"
            android:weightSum="6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="2"
                android:layout_height="wrap_content"
                android:text="服務器IP地址"
                android:textColor="@color/white"
                android:textSize="@dimen/text_normal"
                android:gravity="end"/>
            <com.troila.tdv.ui.widget.ClearableEditText
                android:textColor="@color/white"
                android:inputType="numberDecimal"
                android:id="@+id/et_ip"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:digits="0123456789."
                android:maxLines="1"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <LinearLayout
            android:paddingTop="@dimen/login_top_10"
            android:paddingBottom="@dimen/login_bottom_15"
            android:weightSum="6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="2"
                android:textColor="@color/white"
                android:textSize="@dimen/text_normal"
                android:layout_height="wrap_content"
                android:text="端口號"
                android:gravity="end"/>
            <com.troila.tdv.ui.widget.ClearableEditText
                android:textColor="@color/white"
                android:inputType="number"
                android:maxLines="1"
                android:id="@+id/et_port"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:layout_width="0dp"
                android:layout_weight="3"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <Button
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:textStyle="bold"
            android:text="下一步"
            android:layout_marginBottom="@dimen/login_bottom_15"
            android:background="@drawable/selector_login"
            android:textColor="@color/white"
            android:textSize="@dimen/text_normal"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <android.support.v4.widget.Space
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="6"/>
</LinearLayout>

更多精彩請關注微信公衆賬號likeDev
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章