在一个水平方向的 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)和它在仿真时看起来恰到好处。