翻译:The Space View
Android 4.0版本引入了一个新的名为Space的View。本文包含了这个试图的总结。
“Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.”
这个视图唯一的目的是与布局有关,它会在其他视图间增加距离。
这里有其他的方法来设置间隔,比如paddings和margins,他们可以用在视图上。但是space视图仍然被视为一个不错的基石,因为它可以帮助打破代码的复杂性。在应用中,如果移除有关间隔的代码段,一些高级视图的代码将会更清晰一些。
下面的例子在两个text间增加了一个小间隔。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/first_row" />
<Space
android:layout_width="match_parent"
android:layout_height="20dip" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_row" />
</LinearLayout>
更复杂一些的例子是横向在两个文本视图间增加间隔视图。相对于线性布局中其他组件,间隔视图被设置了一个占比。这意味着,它占用了所有剩余空间,并将文本视图挤到了屏幕的左右两边。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Justified" />
<Space
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Justified" />
</LinearLayout>
参考1:Android 4.0 APIs
参考2:Space
参考3:New Layout Widgets: Space and GridLayout