2010-11-29 142 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>