2010-11-29 167 views
63

我已经到位Android的布局右对齐

<?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个图像按钮。前两个,我想紧挨着对方左对齐。第三个,我想与右边对齐。

现在,前2个按钮是我希望它们成为的地方,但第3个按钮保持左对齐。我将如何让它正确对齐。

回答

124

布局效率极低,而且臃肿。你不需要那么多的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> 
+0

谢谢你,救了我很多麻烦。 – 2016-01-24 02:09:57

18

你可以使用只有一个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参数。我只是展示了你必须提供的基本参数。你可以完成其余的。

3

这是一个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中),你只需简单地必须改变的RelativeLayout的LinearLayout

3

如果您想使用LinearLayout,您可以使用layout_weightSpace元素进行对齐。

E.g.以下布局则以textView和相邻textView2textView3将是右对齐

<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> 

也可以达到同样的效果,而不Space如果你设置layout_weighttextView2。只是我喜欢更加分离的东西,并且还要演示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" /> 

注意you should (not must though) setlayout_width明确,因为它会根据它的重量反正重新计算(你应该设置在垂直LinearLayout元素高度相同)。有关其他布局性能技巧,请参阅Android Layout Tricks系列。

1

为了支持旧版本空间可以替换为视图如下。在最左边的组件之后和最右边的组件之间添加此视图。此重量= 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>