【问题标题】:Background image of ActivityActivity 的背景图片
【发布时间】:2012-12-26 07:39:37
【问题描述】:

我如何将图像包含为背景以及在应用程序中放置图像的位置和方式,我正在使用 Eclipse 和所有开发工具。

这是我在 main_activity.xml 中到目前为止所做的

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#C0C0C0"
       tools:context=".MainActivity">
    </RelativeLayout>

【问题讨论】:

  • android:background="#c0c0c0" 是我现在设置的背景颜色,一旦我让图像背景工作,我就会删除该行
  • 将颜色更改为 android:background="@drawable/your_background_img"
  • 您必须将图像保存在项目的 res/drawable 文件夹中

标签: android eclipse android-layout android-emulator android-2.2-froyo


【解决方案1】:

把你的图片放在 res/drawable 目录中

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

</RelativeLayout>

【讨论】:

    【解决方案2】:

    只需搜索,您就会得到它。

    只需将您的图像放入res/drawable 文件夹并更改

    android:background="#C0C0C0"
    

    android:background="@drawable/your_image_name" (dont give extension of image)
    

    【讨论】:

      【解决方案3】:

      在 res/drawable 文件夹中放一个 .jpg 或 .png 图像,并将背景属性更改为:

      android:background="@drawable/image-name"
      

      其中image-name 是图像的实际名称。

      如您所见,有几个可绘制文件夹,例如 drawable-mdpi 或 drawable-hdpi。它们旨在为具有不同屏幕密度的设备存储图像。为确保您的应用程序在不同设备上看起来不错,请为每种密度提供替代图像。 @drawable/image-name 属性不需要更改,Android 将为您选择正确的可绘制文件夹。希望这会有所帮助。

      【讨论】:

      • @Gaurav,不过,它们是有效的格式。当然最好使用PNG。
      【解决方案4】:

      您只能使用.jpg.png 格式的图像。在那之后, 复制您的图像并将其粘贴到应用程序的res 文件夹中。 如果你的图片名称是image.png

      使用此代码..

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

      【讨论】:

      • @PadmaKumar,是的,这是正确的,如果您想在具有其他背景颜色的 ImageView 中使用您的图像,那么您可以使用。 android:background="#000" (#000= 黑色) android:src="@drawable/image.png"
      • 哦,我问我们可以做“@drawable/image.png”吗? Ans 是没有,不需要提及文件扩展名。感谢您的编辑。
      • @PadmaKumar::- ohkies .. 我没有得到你的问题,这就是原因。但是是的,如果你使用扩展,它会给你一个错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 2016-04-22
      • 2011-04-08
      • 1970-01-01
      相关资源
      最近更新 更多