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

删除回忆录丶 提交于 2019-12-19 03:11:17

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/3030194/android-how-to-have-a-centered-background-image-in-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!