1

在一个水平方向的 LinearLayout 中有两个按钮,它们看起来像这样:

在此处输入图像描述

如果我在按钮之前添加一个“Space”小部件,那么 LinearLayout xml 就是这样的:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2" />

    <Button
        android:id="@+id/btnFetch"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Fetch" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Cancel" />
</LinearLayout>

...我对它的外观很满意,按钮“右对齐”:

在此处输入图像描述

...但这是“正确”的做法吗?

更新

在仿真时,“取消”按钮的文本换成了两行,所以我为它添加了一个“行”属性并调整了权重值(而不是 2、1、1,它们现在是 42、29、29)和它在仿真时看起来恰到好处。

4

1 回答 1

1

我不知道这Space门课,谢谢你发现了那个。

无论如何,不​​,我不会这样做。
我倾向于避免添加视图和视图,如果我可以用更少的东西获得相同的结果。

即:添加一个

android:layout_margin="someValueInDP"

到按钮容器。
或者(作为替代)您可以添加它:

android:padding="someValueInDP"

减少小部件数量有助于提高性能

[编辑]

layout_margin和之间的区别padding,众所周知(好吧,他/她应该),边距是视图/容器之外的空间,而填充是它内部的空间。

只是为了清楚细微的区别。

换句话说:设置边距将保留 OUTER 颜色作为“空间”背景,而填充将扩大 LinearLayout 并且将看到它的颜色(如果与它所在的容器不同)。

于 2014-06-05T18:45:35.553 回答