2011-06-22 13 views
5

Obecnie projektuję ButtonBar z 5 przyciskami (wszystkie będą to przyciski ImageButtons, ale na razie tylko 3 z nich są). To mój pierwszy projekt na Androida, więc uczę się, jak idę. Próbuję waga każdego przycisku jednakowo, bez skalowania (mają równe wypełnienie zamiast równej szerokości). To jest mój kod do tej pory:Dzieci w układzie liniowym z równym wypełnieniem

<LinearLayout 
android:id="@+id/back" 
android:orientation="horizontal" 
android:layout_height="50dip" 
android:layout_width="fill_parent" 
android:layout_alignParentBottom="true" 
android:background="#000000" 
style="@android:style/ButtonBar" 
android:weightSum="5" 
> 

    <ImageButton 
    android:id="@+id/info_button" 
    android:padding="20dp" 
    android:background="@drawable/info" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="1" 
    /> 
    <Button 
    android:id="@+id/wishlist_button" 
    android:text="@string/wish_label" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="1" 
    android:padding="20dp" 
    android:textSize="15dip" 
    android:textColor="#b7b7b7"></Button> 
    <Button 
    android:id="@+id/buy_button" 
    android:text="@string/buy_label" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="1" 
    android:padding="20dp" 
    android:textSize="15dip" 
    android:textColor="#b7b7b7"/> 
    <ImageButton 
    android:id="@+id/dislike_button" 
    android:padding="20dp" 
    android:background="@drawable/dislike_button" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="1" 
    /> 
    <ImageButton 
    android:id="@+id/like_button" 
    android:padding="20dp" 
    android:background="@drawable/like_button" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="1" 
    /> 

to jak to wygląda teraz: rozciągnięte ważona: http://i.imgur.com/MAfjp.png

To czego chcę to wyglądać (ściśle): non -stretched równie wyściełane: http://i.imgur.com/vXTEy.png

Dziękuję za pomoc. Szukałem odpowiedzi od jakiegoś czasu i próbowałem już tak wiele.

Odpowiedz

6

Oto ona:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/back" 
    android:orientation="horizontal" 
    android:layout_height="50dip" 
    android:layout_width="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:background="#000000" 
    style="@android:style/ButtonBar" 
    android:gravity="center" 
    > 
    <ImageButton 
    android:id="@+id/info_button" 
    android:background="@null" 
    android:src="@drawable/info" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    /> 
    <Button 
    android:id="@+id/wishlist_button" 
    android:text="@string/wish_label" 
    android:background="@null" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    android:textSize="15dip" 
    android:textColor="#b7b7b7"></Button> 
    <Button 
    android:id="@+id/buy_button" 
    android:text="@string/buy_label" 
    android:background="@null" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    android:textSize="15dip" 
    android:textColor="#b7b7b7"/> 
    <ImageButton 
    android:id="@+id/dislike_button" 
    android:background="@null" 
    android:src="@drawable/dislike_button" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    /> 
    <ImageButton 
    android:id="@+id/like_button" 
    android:background="@null" 
    android:src="@drawable/like_button" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    /> 
</LinearLayout> 

nie wiem jak style = "@ android: styl/paska przycisków" jest ustawiona, więc jeśli nie pokazuje prawidłowo spróbuj ją usunąć.

+0

Awesome! Jakoś to zadziałało! Dziękuję Ci bardzo. – jbenowitz

1

Można dodać element odstępu pomiędzy każdym z obrazów/przycisków i przypisać element layout_weight do elementów odstępu zamiast obrazów/przycisków. Tak to będzie wyglądać mniej więcej tak:

<LinearLayout 
android:id="@+id/back" 
android:orientation="horizontal" 
android:layout_height="50dip" 
android:layout_width="fill_parent" 
android:layout_alignParentBottom="true" 
android:background="#000000" 
style="@android:style/ButtonBar" 
> 

    <ImageButton 
    android:id="@+id/info_button" 
    android:padding="20dp" 
    android:background="@drawable/info" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    /> 
     <LinearLayout 
     android:layout_height="wrap_content"    
     android:layout_weight="1" /> 
    /> 
    <Button 
    android:id="@+id/wishlist_button" 
    android:text="@string/wish_label" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:padding="20dp" 
    android:textSize="15dip" 
    android:textColor="#b7b7b7"></Button> 
     <LinearLayout 
     android:layout_height="wrap_content"    
     android:layout_weight="1" /> 
    <Button 
    android:id="@+id/buy_button" 
    android:text="@string/buy_label" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:padding="20dp" 
    android:textSize="15dip" 
    android:textColor="#b7b7b7"/> 
     <LinearLayout 
     android:layout_height="wrap_content"    
     android:layout_weight="1" /> 
    <ImageButton 
    android:id="@+id/dislike_button" 
    android:padding="20dp" 
    android:background="@drawable/dislike_button" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    /> 
     <LinearLayout 
     android:layout_height="wrap_content"    
     android:layout_weight="1" /> 
    <ImageButton 
    android:id="@+id/like_button" 
    android:padding="20dp" 
    android:background="@drawable/like_button" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    /> 
</LinearLayout> 

To może nie być najbardziej eleganckie rozwiązanie, ponieważ dodaje kilka widoków do układu, ale pracował dla mnie w przeszłości.

+1

złe rozwiązanie IMO – BrainCrash

0

Należy rozważyć użycie tylko przycisku obrazu z obrazami o jednakowej wielkości z jednakową wagą wewnątrz linii LinearLayout.

Pozdrawiam, Stéphane

Powiązane problemy