2010-06-12 6 views
5

Je voudrais utiliser une petite image (de taille d'icône) comme arrière-plan pour ma vue, mais elle est étirée pour remplir toute la vue.Android: comment avoir une image d'arrière-plan centrée dans une vue

Toute solution (y compris un moyen de placer une vue sous mon actuelle et de rendre cette dernière transparente) sera appréciée.

Merci!

code actuel:

<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

comment allez-vous l'image de réglage comme arrière-plan? être précis – mtmurdock

+0

a modifié la question pour inclure un exemple de code – tacone

+0

duplication possible de [Centrer une image de fond dans Android] (http://stackoverflow.com/questions/3847979/centering-a-background-image-in-android) –

Répondre

11

Vous avez deux choix:

  1. rendre l'image plus grande. Plus précisément, ajoutez une seule ligne noire tout autour de ses bords, puis utilisez l'outil tools/draw9patch pour faire de cette ligne noire la partie 'extensible'. Lorsque l'image est mise à l'échelle en arrière-plan, l'icône d'origine ne sera pas étirée.

  2. Abandonnez le android: background et utilisez plutôt un RelativeLayout de haut niveau. Cela devrait fonctionner

<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> 

Hope this helps

Questions connexes