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

  • 相关阅读:
    牛客练习赛44 A 小y的序列 (模拟,细节)
    牛客假日团队赛10 L 乘积最大 (dp,大数)
    三分查找
    几何基础知识点
    POJ 2318 TOYS
    UVA 11916 Emoogle Grid(大步小步算法(解模方程对数) 快速幂 模的逆)
    UVA 11426 GCD
    Aladdin and the Flying Carpet(算术基本定理)
    算术基本定理
    数论总结帖
  • 原文地址:https://www.cnblogs.com/hehe520/p/6329860.html
Copyright © 2020-2023  润新知