Android layout 这是定位小部件的正确方法,还是一种混乱?

Android layout 这是定位小部件的正确方法,还是一种混乱?,android-layout,android-linearlayout,android-layout-weight,Android Layout,Android Linearlayout,Android Layout Weight,在一个水平方向的线性布局中有两个按钮,它们看起来如下所示: 如果我在按钮前添加一个“空格”小部件,那么LinearLayout xml如下所示: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Space android:layou

在一个水平方向的线性布局中有两个按钮,它们看起来如下所示:

如果我在按钮前添加一个“空格”小部件,那么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),它在模拟时看起来正好合适。

我不知道
空格
类,感谢您指出了这一点

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

即:加入

android:layout_margin="someValueInDP"
到按钮容器。
(可选)您可以添加:

android:padding="someValueInDP"
减少小部件数量有助于提高性能

[编辑]

每个人都知道(他/她应该知道),版面空白和填充之间的区别在于,空白是视图/容器外部的空间,而填充是视图/容器内部的空间

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


换句话说:设置边距将保留外部颜色作为“空格”背景,而填充将放大线性布局,并可以看到其颜色(如果与所处容器不同)。

否,空格;它在上面的XML中。