Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to have a centered background image in a view

I'd like to use a small (icon size) image as the background for my view, but it gets stretched to fill all the view.

Any solution (including some way to place a view under my current one and make the latter transparent) will be appreciated.

Thanks !

current code:

<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"
 >
like image 549
tacone Avatar asked Jun 12 '10 21:06

tacone


1 Answers

You have two choices:

  1. Make the image bigger. Specifically, add a single black line all around its edges, then use the tools/draw9patch tool to make that black line the 'stretchable' part. When the image is scaled up to the background, your original icon will not be stretched.

  2. Give up on the android:background and use a top level RelativeLayout instead. This should work

<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

like image 95
Charles Merriam Avatar answered Jan 04 '23 05:01

Charles Merriam