2010-11-29 19 views
63

Mam następujący układ w miejscuAndroid Układ Right Align

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

Dla celów tego pytania, jestem tylko o dolnej części układu. Obecnie zawiera 3 przyciski obrazowe. Pierwsze 2, chcę obok siebie wyrównane do lewej. Trzeci, chcę być wyrównany do prawej strony.

Tak jak dwa pierwsze przyciski są tam, gdzie chcę je mieć, ale trzecia z nich jest trwale utrzymywana w lewo. Jak mam to ustawić w prawo?

Odpowiedz

124

Układ jest niezwykle nieefektywny i nadęty. Nie potrzebujesz tak wielu LinearLayout s. W rzeczywistości wcale nie potrzebujesz żadnego LinearLayout.

Należy używać tylko jednego RelativeLayout. Lubię to.

<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

Dziękuję, uratował mi wiele kłopotów. –

18

Możesz to wszystko zrobić, używając tylko jednego parametru RelativeLayout (który, btw, nie wymaga parametru android:orientation). Tak więc, zamiast mieć LinearLayout, zawierający kilka rzeczy można zrobić coś takiego:

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

Jak zauważył, istnieją pewne parametry XML brakuje. Właśnie pokazywałem podstawowe parametry, które musiałeś umieścić. Możesz dokończyć resztę.

3

To jest przykład dla 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); 

Z innego rodzaju układu (przykład LinearLayout), które po prostu musi się zmienić RelativeLayout dla LinearLayout.

3

Jeśli chcesz użyć LinearLayout, możesz wykonać wyrównanie z layout_weight z elementem Space.

E.g. następujący układ umieszcza textView i textView2 obok siebie i textView3 będzie wyrównany do prawej

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

można osiągnąć ten sam efekt bez Space gdyby ustawić layout_weight do textView2. Chodzi o to, że lubię rzeczy bardziej oddzielone, a także, aby pokazać element 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" /> 

Zauważ, że you should (not must though) setlayout_width wyraźnie, jak zostanie ona przeliczona według jego waga mimo to (w ten sam sposób należy ustawić wysokość w elementach pionowych LinearLayout). Aby zapoznać się z innymi poradami dotyczącymi wydajności układu, zobacz serię Android Layout Tricks.

1

Do obsługi starszej wersji Przestrzeń można zastąpić widokiem jak poniżej. Dodaj ten widok między po lewej stronie najbardziej komponentu i przed prawym prawym elementem. Ten widok z wagą = 1 będzie rozciągał się i wypełniał przestrzeń

Tutaj podano pełny kod przykładowy. Ma 4 komponenty.Dwie strzałki będą znajdować się po prawej i lewej stronie. Tekst i Spinner będą na środku.

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