2010-06-12 8 views
5

Me gustaría utilizar una imagen pequeña (tamaño de icono) como fondo de mi vista, pero se estira para llenar toda la vista.Android: cómo tener una imagen de fondo centrada en una vista

Cualquier solución (incluyendo alguna forma de colocar una vista debajo de la actual y hacer que esta última sea transparente) será apreciada.

Gracias!

código actual:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/icon" 
    android:id="@+id/layout_main" 
> 
+0

cómo están estableciendo que la imagen como fondo? sea ​​específico – mtmurdock

+0

editó la pregunta para incluir un código de muestra – tacone

+0

posible duplicado de [Centrar una imagen de fondo en Android] (http://stackoverflow.com/questions/3847979/centering-a-background-image-in-android) –

Respuesta

11

Usted tiene dos opciones:

  1. hacer la imagen más grande. Específicamente, agregue una sola línea negra alrededor de sus bordes, luego use la herramienta tools/draw9patch para hacer que esa línea negra sea la parte "estirable". Cuando la imagen se amplía hasta el fondo, su icono original no se alargará.

  2. Renunciar al android: fondo y utilizar un RelativeLayout de nivel superior en su lugar. Esto debería funcionar

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageSwitcher 
     android:id="@+id/ImageSwitcher01" 
     android:layout_width="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon"> 
    </ImageSwitcher> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true"> 
     <TextView 
      android:text="Your old top level here" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"> 
     </TextView> 
    </LinearLayout> 
</RelativeLayout> 

Esperanza esto ayuda

Cuestiones relacionadas