日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

[Android]Space控件的应用场景

發布時間:2025/3/8 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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.*/@Overridepublic 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;}@Overrideprotected 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,而不是使用默認值

<activityandroid: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"><includeandroid:id="@+id/toolbar"layout="@layout/toolbar" /><android.support.v4.widget.Spaceandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="4"/><LinearLayoutandroid:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:background="@drawable/setting_bg"><TextViewandroid: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"/><LinearLayoutandroid:paddingTop="@dimen/login_top_20"android:paddingBottom="@dimen/login_bottom_15"android:weightSum="6"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid: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.ClearableEditTextandroid: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><LinearLayoutandroid:paddingTop="@dimen/login_top_10"android:paddingBottom="@dimen/login_bottom_15"android:weightSum="6"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid: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.ClearableEditTextandroid: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><Buttonandroid: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.Spaceandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="6"/> </LinearLayout> 復制代碼

更多精彩請關注微信公眾賬號likeDev。

總結

以上是生活随笔為你收集整理的[Android]Space控件的应用场景的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。