2010-06-12 87 views
5

我想使用一个小的(图标大小)图像作为我的视图的背景,但它会被拉伸以填充所有视图。Android:如何在视图中使用居中的背景图像

任何解决方案(包括某种方式来放置在我目前的一个视图,并使后者透明)将不胜感激。

谢谢!

当前代码:

<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

你是如何设置图像作为背景?请具体 – mtmurdock 2010-06-12 22:34:07

+0

编辑该问题以包含示例代码 – tacone 2010-06-12 22:53:56

+0

[在Android中定位背景图像]可能的副本(http://stackoverflow.com/questions/3847979/centering-a-background-image-in-android) – 2012-06-28 13:01:15

回答

11

你有两个选择:

  1. 使图像更大。具体而言,在其边缘添加一条黑色线条,然后使用工具/ 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> 

希望这有助于