Android布局右alignment

我有以下布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="13"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" /> </LinearLayout> </LinearLayout> <RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" > <ImageButton android:background="@null" android:id="@+id/special" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/> </RelativeLayout> </LinearLayout> </LinearLayout> 

就这个问题而言,我只关心布局的下半部分。 现在它包含3个图像button。 第一个2,我要紧挨着对方左alignment。 第三个,我想和右边alignment。

现在,前2个button是我想要它们的位置,但是第3个button保持左alignment。 我将如何正确alignment。

布局是非常低效和臃肿。 你不需要那么多的LinearLayout 。 实际上,您根本不需要任何LinearLayout

只使用一个RelativeLayout 。 喜欢这个。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" android:layout_alignParentLeft="true"/> <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" android:layout_toRightOf="@id/back"/> <ImageButton android:background="@null" android:id="@+id/special" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_alignParentRight="true"/> </RelativeLayout> 

你可以通过使用一个RelativeLayout来完成所有这一切(其中,顺便说一句,不需要android:orientation参数)。 所以,而不是有一个LinearLayout ,包含一堆东西,你可以做这样的事情:

 <RelativeLayout> <ImageButton android:layout_width="wrap_content" android:id="@+id/the_first_one" android:layout_alignParentLeft="true"/> <ImageButton android:layout_width="wrap_content" android:layout_toRightOf="@+id/the_first_one"/> <ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true"/> </RelativeLayout> 

正如你注意到的,有一些XML参数丢失。 我只是显示了你必须提供的基本参数。 你可以完成其余的。

这是一个RelativeLayout的例子:

 RelativeLayout relativeLayout=(RelativeLayout)vi.findViewById(R.id.RelativeLayoutLeft); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams(); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); relativeLayout.setLayoutParams(params); 

使用另一种布局(例如LinearLayout),您只需更改LinearLayout的 RelativeLayout即可

如果你想使用LinearLayout ,你可以使用Space元素与layout_weightalignment。

例如下面的布局将textViewtextView2放在一起, textView3将右alignment

 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView2" /> <Space android:layout_width="0dp" android:layout_weight="1" android:layout_height="20dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView3" /> </LinearLayout> 

如果您将layout_weight设置为layout_weight ,则可以在没有Space情况下实现相同的效果。 只是我喜欢更加分离的东西,再加上展示Space元素。

  <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView2" /> 

请注意, 您应该(但不是必须)明确地设置 layout_width因为无论如何将根据它的权重重新计算layout_width (与在垂直LinearLayout元素中设置高度的方式相同)。 有关其他布局性能提示,请参阅Android布局技巧系列。

要支持较旧的版本空间可以replace为视图如下。 在最左边的组件之后和最右边的组件之间添加此视图。 这个重量= 1的视图将伸展并填充空间

  <View android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" /> 

完整的示例代码在这里给出。 它有4个组件。 两个箭头将在右侧和左侧。 文本和微调将在中间。

  <ImageButton android:id="@+id/btnGenesis" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center|center_vertical" android:layout_marginBottom="2dp" android:layout_marginLeft="0dp" android:layout_marginTop="2dp" android:background="@null" android:gravity="left" android:src="@drawable/prev" /> <View android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" /> <TextView android:id="@+id/lblVerseHeading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center" android:textSize="25sp" /> <Spinner android:id="@+id/spinnerVerses" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:textSize="25sp" /> <View android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" /> <ImageButton android:id="@+id/btnExodus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center|center_vertical" android:layout_marginBottom="2dp" android:layout_marginLeft="0dp" android:layout_marginTop="2dp" android:background="@null" android:gravity="right" android:src="@drawable/next" /> </LinearLayout>