[Android]Space控件的应用场景

简介: Space控件是在Android 4.0中加入,是个空白的view,一般用于填充View组件中的间隙。support-v4包里提供了兼容低版本的Space控件。源码分析Space控件源码非常简单,先来看看public class Space extends View { public Space(Context context, Attrib

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
这里写图片描述

相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
40 1
|
4月前
|
Java Android开发
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
157 0
|
1月前
|
Android开发
[Android]RadioButton控件
[Android]RadioButton控件
12 0
|
3月前
|
Android开发
分享88个Android控件源代码总有一个是你想要的
分享88个Android控件源代码总有一个是你想要的
21 0
|
3月前
|
Android开发
分享89个Android控件源代码总有一个是你想要的
分享89个Android控件源代码总有一个是你想要的
71 0
|
4月前
|
XML Android开发 数据格式
[Android]开关控件Switch
[Android]开关控件Switch
35 0
|
4月前
|
Android开发 Kotlin
android开发,使用kotlin学习滚动控件RecyclerView
android开发,使用kotlin学习滚动控件RecyclerView
36 0
|
4月前
|
XML Java Android开发
Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)
Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)
71 0
|
8月前
|
缓存 Java API
#7,Android开发 控件 ProgressBar 进度条
#7,Android开发 控件 ProgressBar 进度条
|
8月前
|
Android开发
#6,Android Studio Android 开发控件 显示图片 ImageView的使用
#6,Android Studio Android 开发控件 显示图片 ImageView的使用