Android:如何在 View 中设置居中背景图片

标签 android views background

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

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

谢谢!

当前代码:

<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. 将图像放大。具体来说,在其边缘周围添加一条黑线,然后使用 tools/draw9patch 工具使该黑线成为“可拉伸(stretch)”部分。当图像放大到背景时,您的原始图标将不会被拉伸(stretch)。

  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>

希望对你有帮助

关于Android:如何在 View 中设置居中背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3030194/

相关文章:

android - BroadcastReceiver 尝试在无序广播期间返回结果 - Android 中的 PACKAGE_ADDED

android - 根据第一个 Activity 更改第二个 Activity 的标题

android - 为什么我的 onCreateView 方法被调用了两次?

ruby-on-rails - 如何对我的 Rails 应用程序的 CRUD 部分进行 DRY?

android 编辑文本背景

Android:如何检查应用程序是否已进入后台以避免不需要的 BroadcastReceiver 检查

performance - View 和表在性能上的区别

php - 在 MySQL 中存储 View /统计信息的最佳方式

CSS - 为什么不是 :hover parent hide child elements

html - 100% 宽度背景图片,高度为 'auto' (2015) - 跨浏览器