2012-02-26 54 views
13

Necesito hacer mi propia barra de herramientas en la aplicación de Android. Ahora se ve así: enter image description hereEliminar espacio entre botones en LinearLayout (Android)

Para ver espacios entre botones. Traté de poner margen negativo/relleno en los botones, pero el espacio no desapareció.

Aquí es código de diseño:

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

    <ListView 
     android:id="@+id/routes_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5"> 
    </ListView> 

    <TableRow 
     android:id="@+id/toolbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:padding="0dp" > 

     <Button 
      android:id="@+id/btn_routes" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dp" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-10dp" 
      android:layout_marginLeft="-10dp" 
      android:text="@string/routes" 
      android:textSize="10dp" /> 

     <Button 
      android:id="@+id/btn_places" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dp" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-10dp" 
      android:layout_marginLeft="-10dp" 
      android:textSize="10dp" 
      android:text="@string/places" /> 

     <Button 
      android:id="@+id/btn_events" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dp" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-10dp" 
      android:layout_marginLeft="-10dp" 
      android:textSize="10dp" 
      android:text="@string/events" /> 

     <Button 
      android:id="@+id/btn_about" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="0dp" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-10dp" 
      android:layout_marginLeft="-10dp" 
      android:text="@string/about" 
      android:textSize="10dp" /> 

    </TableRow> 

</LinearLayout> 

Cómo eliminar el espacio entre los botones?

+0

Tal vez se puede comprobar esto - http://stackoverflow.com/questions/2277732/how-to-get-rid-of-the-extra-gap-between-a-button-and-other-views –

+0

La idea es utilizar ImageButtons con el mismo color de fondo ... – hovanessyan

Respuesta

8

he aquí una solución. Parece que me apresuré a publicar una pregunta aquí.

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

    <ListView 
     android:id="@+id/routes_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5"> 
    </ListView> 

    <TableRow 
     android:id="@+id/toolbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom= "-7dp"> 

     <Button 
      android:id="@+id/btn_routes" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-4.5dp" 
      android:layout_marginLeft="-5dp" 
      android:text="@string/routes" 
      android:textSize="10dp" /> 

     <Button 
      android:id="@+id/btn_places" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-4.5dp" 
      android:layout_marginLeft="-4.5dp" 
      android:textSize="10dp" 
      android:text="@string/places" /> 

     <Button 
      android:id="@+id/btn_events" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-4.5dp" 
      android:layout_marginLeft="-4.5dp" 
      android:textSize="10dp" 
      android:text="@string/events" /> 

     <Button 
      android:id="@+id/btn_about" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:layout_marginRight="-4dp" 
      android:layout_marginLeft="-4.5dp" 
      android:text="@string/about" 
      android:textSize="10dp" /> 

    </TableRow> 

</LinearLayout> 

Resultado: enter image description here

+6

No creo que esto funcione en todos los dispositivos. –

+0

No es una buena respuesta, con un estilo de botón diferente. Los valores serán diferentes. – xmen

0

probar esto

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

    <ListView 
     android:id="@+id/routes_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.5"> 
    </ListView> 

    <TableRow 
     android:id="@+id/toolbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:padding="0dp" > 

     <Button 
      android:id="@+id/btn_routes" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/routes" 
      android:textSize="10dp" /> 

     <Button 
      android:id="@+id/btn_places" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="10dp" 
      android:text="@string/places" /> 

     <Button 
      android:id="@+id/btn_events" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="10dp" 
      android:text="@string/events" /> 

     <Button 
      android:id="@+id/btn_about" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/about" 
      android:textSize="10dp" /> 

    </TableRow> 

</LinearLayout> 
0
I won't suggest to go for -ve margins.. 

configuración más bien una imagen de fondo para el botón.

Algunos puntos importantes: -

  1. La imagen de fondo en sí no deben contener ningún espacio en blanco en las fronteras.
  2. Debe crear un archivo selector para ese botón después de proporcionar el fondo, en el que describirá qué imagen mostrar en diferentes estados de botón del mismo modo selected, focused etc. De lo contrario, no podrá ver los efectos que aparece de forma predeterminada (como el botón de plata y cuando se hace clic se pone de color naranja)

Soluciones: -

  1. ¿Cómo crear un selector se puede hacer referencia this lik
  2. he utilizado this button in my case

Mi Resultado: -

enter image description here

-2

Añadir estas dos líneas en todo el botón y usa la disposición lineal en lugar de TableLayout con orientación horizontal.

android:layout_width="0dp" 
android:button_weight="1" 
19

trate de cambiar el color del botón, ya que la interfaz por defecto del botón que es nativa de Android es en realidad más pequeño que su tamaño, y es de centro-montado para que se vea fresca.

Cambia el fondo a negro o algo así y verás el tamaño real del botón.

androide: Fondo = "# 000"

+1

Esto realmente debería ser la respuesta aceptada. No parece que haya un margen real entre el botón y otros botones, solo el fondo que se aplica tiene algo de relleno. – CodyEngel

+0

Guau, de verdad ... Pasé bastante tiempo averiguando por qué no podía deshacerme del espacio/margen entre los botones en mi Gridview, cambiando el fondo como me dijiste, que realmente no había espacio/margen entre ellos nunca más, gracias por esta respuesta. –

+1

Los botones utilizan un parche de 9 parches que hace que parezca que hay un margen adicional alrededor. –

0

Simplemente, puede hacerlo reemplazando el fondo por defecto con tiene una forma redondeada y el margen:

<Button 
      android:id="@+id/button_1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      <!--Replace the following color with any other color or drawable--> 
      android:background="@android:color/white" 
      android:text="@string/button1_text" 
      android:textColor="@android:color/black" /> 
Cuestiones relacionadas