2013-08-03 12 views
13
<Button 
    android:id="@+id/o_pharmacy" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:drawableLeft="@drawable/p2" 
    android:text="@string/o_pharmacy" 
    android:textSize="26sp" /> 

<Button 
    android:id="@+id/lab" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:drawableLeft="@drawable/lab" 
    android:text="@string/lab" 
    android:textSize="26sp" /> 

<Button 
    android:id="@+id/i_pharmacy" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:drawableLeft="@drawable/p1" 
    android:text="@string/i_pharmacy" 
    android:textSize="26sp" /> 

Próbowałem powyższego kodu, aby wyświetlić 3 przyciski w układzie Liner. Działa, ale muszę umieścić spację między dwoma przyciskami.Umieść przestrzeń między przyciskami androida

+0

jaka jest orientacja twojego LinearLayout? –

Odpowiedz

22
android:layout_margin="10dp" 

dla każdego przycisku

3
<Button 
    android:id="@+id/o_pharmacy" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:drawableLeft="@drawable/p2" 
    android:text="@string/o_pharmacy" 
    android:textSize="26sp" /> 

<Button 
    android:id="@+id/lab" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="25dp" 
    android:drawableLeft="@drawable/lab" 
    android:text="@string/lab" 
    android:textSize="26sp" /> 

<Button 
    android:id="@+id/i_pharmacy" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="25dp" 
    android:drawableLeft="@drawable/p1" 
    android:text="@string/i_pharmacy" 
    android:textSize="26sp" /> 

Spróbuj tego.

12

Jeżeli orientacja swojej LinearLayout jest pionowa, użyj

android:layout_marginTop="10dp" 

inaczej, należy

android:layout_marginLeft="10dp" 
0

Lepiej używać

android:textSize="26dp" 

zamiast

android:textSize="26sp" 

, aby uzyskać lepszy wynik na wszystkich urządzeniach o różnych rozmiarach!

+2

Nie. 'sp' jest wymagany dla rozmiarów tekstu –

0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
       android:id="@+id/o_pharmacy" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 

       android:layout_marginTop="10dip" 

       android:drawableLeft="@drawable/p2" 
       android:text="@string/o_pharmacy" 
       android:textSize="26sp" /> 

        <Button 
        android:id="@+id/lab" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 

        android:layout_marginTop="10dip" 

        android:drawableLeft="@drawable/lab" 
        android:text="@string/lab" 
        android:textSize="26sp" /> 

        <Button 
        android:id="@+id/i_pharmacy" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 

        android:layout_marginTop="10dip" 

        android:drawableLeft="@drawable/p1" 
        android:text="@string/i_pharmacy" 
        android:textSize="26sp" /> 

jestem przy założeniu, że używasz orientacji pionowej dla LinearLayout, inaczej ten kod nie będzie sensu jak twoje przyciski są Fill_parent dla layout_width. Zwróć uwagę na linię, która mówi: android:layout_marginTop="10dip", co zapewnia, że ​​pozostawisz rozsądną 10 przerw między przyciskami. Oczywiście, możesz zwiększyć (lub zmniejszyć) tę przestrzeń pomiędzy przyciskami. To Twój wybór.

Mam nadzieję, że to zadowalająco odpowiedziała na twoje pytanie.

0
android:layout_marginBottom="50dp" 

android:layout_marginTop="50dp" 
0

Najlepiej jest korzystać z Androidem: layout_marginTop = „10dp” w swojej działalności XML, jak to daje dokładny odstęp między tym przycisku i innego przycisku lub widgetu. Powtórz to dla pozostałych przycisków. Happy Programming!

+0

Czy jest to powtórzenie [tej istniejącej odpowiedzi] (http://stackoverflow.com/questions/18029378/put-space-between-android-buttons/18029400#18029400)? – Pang

1

Najprostszym sposobem, aby przestrzeń pomiędzy 3 przyciski w poziomym LinearLayout używać tego przycisku centrum:

android:layout_marginLeft="10dp" 
android:layout_marginRight="10dp" 

Jeśli masz pionowe LinearLayout, można użyć marginTop i marginBottom.

Powiązane problemy