Space View

翻译: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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值