【问题标题】:How to stretch a single image as background in android layout?如何在android布局中拉伸单个图像作为背景?
【发布时间】:2013-09-20 05:47:27
【问题描述】:

主屏幕仅包含搜索,我希望背景图像成为我的徽标。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/image"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="178dp"
    android:layout_marginLeft="40dp"
    android:ems="10" />

<SearchView
    android:id="@+id/searchView1"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/editText1" >

</SearchView>

我需要显示完整尺寸的图像,此代码以平铺格式显示我的徽标的多个副本。

【问题讨论】:

  • 这段代码有什么问题。

标签: android image background


【解决方案1】:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/image" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </RelativeLayout>
</RelativeLayout>

您也可以使用android:scaleType="centerCrop",而不是使用android:scaleType="fitXY"。 更多信息请阅读Android Scale Type

【讨论】:

    【解决方案2】:

    您需要使用 0.9 的补丁图像来拉伸并使其完美的屏幕适配,而无需拉伸瓷砖。从draw9patch创建9个补丁图像或者网上也有很多工具,只需在google上写你就会得到它,并制作android:scaleType =“fitXY”。

    【讨论】:

      【解决方案3】:

      您的问题可能是您要用作拉伸背景的图像不是 9patch。 9patchdrawer 包含在 Android SDK 中(但您也可以使用任何图像编辑器) 这里重要的是 1px 黑线告诉 android 哪些部分用于拉伸(左行和底行),哪些部分用于保留内容。

      然后将图像保存为 .9.png(android 会自动将其识别为可拉伸图像)

      这里是如何绘制 9-patch 的链接 http://developer.android.com/tools/help/draw9patch.html

      【讨论】:

        【解决方案4】:

        你应该删除它;

        android:background="@drawable/image"

        并将其添加到RelativeLayout中;

        <ImageView
            android:layout_width="wrap_parent"
            android:layout_height="wrap_parent"
            android:layout_centerInParent="true"
            android:src="@drawable/image" />
        

        【讨论】:

          【解决方案5】:

          您需要 9-patch 图像将单个图像拉伸为 android 布局中的背景。图像扩展名为 .9.png。您使用存储在 android-sdk-windows\tools\draw9patch.bat 文件夹中的 9-patchdrawer 制作这种类型的图像。

          我觉得应该对你有用。

          【讨论】:

            【解决方案6】:

            从您的代码中删除这些行

            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            

            并尝试在屏幕上显示完整尺寸的图像!

            【讨论】:

            • 不,我仍然无法解决同样的问题
            • 我检查了你的代码!!我得到了一个完整尺寸的图像,在中心搜索视图和编辑文本中!!!更好的是你可以编辑徽标并将其放置在背景中!!
            • 否则用imageview!!会更好!!
            猜你喜欢
            • 2011-12-27
            • 1970-01-01
            • 2010-11-09
            • 1970-01-01
            • 2012-09-19
            • 2011-11-12
            • 2011-06-07
            • 2010-12-09
            • 2017-01-18
            相关资源
            最近更新 更多