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

前端 未结 1 2022
感动是毒
感动是毒 2021-01-05 16:45

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 vie

相关标签:
1条回答
  • 2021-01-05 17:20

    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

    0 讨论(0)
提交回复
热议问题