linearlayout水平靠右显示方法

在安卓中,只有垂直的linearlayout属性里面的内部组件靠左靠右才有效,同理,水平的linearlayout属性里面的内部组件靠上靠下才有效,否则无效由此考虑如何让水平linearlayout中同一行内组件靠左靠右显示呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#acc"
android:orientation="horizontal">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="3.0dip"
android:background="@drawable/move_on1" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="3.0dip"
android:background="@drawable/stopa" />
</LinearLayout>
</LinearLayout>

参考资料:也谈layout_gravity和gravity
界面布局汇总和解析

另外一种解决方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#acc"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="3.0dip"
android:background="@drawable/move_on1" />

<!--用一个空的view把中间撑开-->
<View
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="3.0dip"
android:background="@drawable/stopa" />

</LinearLayout>