Android:如何在视图中设置居中的背景图像

tac*_*one 6 android background views

我想使用一个小的(图标大小)图像作为我的视图的背景,但它会被拉伸以填充所有视图.

我们将不胜感激任何解决方案(包括将视图置于我当前视图之下并使后者透明的某种方式).

谢谢 !

当前代码:

<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"
 >
Run Code Online (Sandbox Code Playgroud)

Cha*_*iam 12

你有两个选择:

  1. 使图像更大.具体来说,在其边缘添加一条黑色线条,然后使用tools/draw9patch工具将黑色线条变为"可伸缩"部分.将图像放大到背景时,原始图标将不会拉伸.

  2. 放弃android:background并使用顶级RelativeLayout代替.这应该工作

<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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助