【问题标题】:Android: how to have a centered background image in a viewAndroid:如何在视图中有一个居中的背景图像
【发布时间】:2013-08-31 06:47:18
【问题描述】:

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

任何解决方案(包括将视图放置在我当前的视图下并使后者透明的某种方式)都将受到赞赏。

谢谢!

当前代码:

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

【问题讨论】:

  • 如何将图像设置为背景?具体
  • 编辑问题以包含示例代码
  • @grace-shao:我看不出编辑/改进问题中实际代码的基本原理。

标签: android views background


【解决方案1】:

你有两个选择:

  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>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2011-04-20
    • 2023-03-05
    • 2011-06-29
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2015-09-23
    相关资源
    最近更新 更多