2012-04-27 14 views
15

Mam ImageView i ImageButton. Mam je obok siebie w układzie poziomym. Staram się, aby obraz był wyrównany do lewej na ekranie, a przycisk jest wyrównany do prawej. Próbowałem ustawić grawitację, ale nie robi to różnicy. Gdzie się mylę?Układ systemu Android lewa i prawa wyrównania w układzie poziomym

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:layout_weight="1" 
     android:gravity="left" 
     android:src="@drawable/short_logo" /> 

    <ImageButton 
     android:id="@+id/terminateSeed" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="right" 
     android:background="@null" 
     android:src="@drawable/unregister_icon" /> 
</LinearLayout> 

Odpowiedz

29
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ic_launcher" /> 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="Button" /> 

</RelativeLayout> 
+0

używać względnych układ to zawsze działa – MAC

+9

jest to, należy zawsze wydają się odpowiedzieć na pytanie w sposób, z którym była zapytał, można to było zrobić łatwo za pomocą LinearLayout, ustawiając właściwość weightSum ... –

+1

tak masz rację @mirroredAbstraction , ale staraj się dać z siebie wszystko. i LinearLayout niezbyt przydatne do prawdziwego rozwoju, sugerują mu użycie Relative one ... – MAC

0
<RelativeLayout 
android:id="@+id/linearLayout1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 

> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 

    android:layout_alignParentLeft="true" 
    android:src="@drawable/short_logo" /> 

<ImageButton 
    android:id="@+id/terminateSeed" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:background="@null" 
    android:src="@drawable/unregister_icon" /> 
</RelativeLayout > 
10

Zastosowanie ...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="1.0" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 
    android:layout_marginRight="80dp" 
    android:layout_weight="0.5" 
    android:gravity="left" 
    android:src="@drawable/ic_launcher" /> 

<ImageButton 
    android:id="@+id/terminateSeed" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:gravity="right" 
    android:layout_marginLeft="80dp" 
    android:background="@null" 
    android:src="@drawable/ic_launcher" /> 
</LinearLayout> 
+0

Ja to nie zadziała –

+0

@Agarwal, dlaczego tak myślisz, czy próbowałeś? –

+2

działa bardzo dobrze, a poza tym ... to powinna być poprawna odpowiedź ... ponieważ pytanie było ... jak rozwiązać problem z LinearLayout ... nie oferować alternatyw z RelativeLayout ... Wiem też z RelativeLayout ... ale potrzebuję LinearLayout ... To musi być poprawna odpowiedź –

0

Bądź imageview i imagebuttonwrap_content i ustawić layout_gravity na lewo i prawo odpowiednio.

0

RelativeLayout rozwiązałoby problem z trzaskiem, ale jeśli nadal nalegać na użyciu LinearLayout (tylko dlatego, że jesteśmy hardkorowy i robić rzeczy w przykry sposób), robisz to tak:

rodzica LinearLayout jest wypełniony, wyrównany do lewej i zorientowany poziomo, i zawiera twój ImageView i inny LinearLayout.

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:gravity="left" 

Drugi LinearLayout jest wypełniony, wyrównany do prawej i zorientowany poziomo i zawiera Twój ImageButton.

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:gravity="right" 
2

korzystania z tego przykładu, jeśli chcesz używać LinearLayout bez podania Waga

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:gravity="right" 
     /> 
    </LinearLayout> 
Powiązane problemy