0

截至今天,我有点像 Android 菜鸟。

我尝试将图片设置为背景,如下所示;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="C:\Users\Jacob\Downloads\xmas.jpg"
android:layout_height="match_parent"
tools:context=".MainActivity" >

但是当我构建它时,它会出现;

'描述资源路径位置类型错误:错误:不允许字符串类型(在'背景',值为'C:\ Users \ Jacob \ Downloads \ xmas.jpg')。activity_main.xml /FirstAndroidApp/res/layout 第 1 行 Android AAPT 问题'

当我进入设计器时,它会显示我想要的背景,但只是出现错误。

我不允许以这种方式设置背景吗?还是我错过了什么?

4

3 回答 3

1

您必须将图片复制到您的drawable文件夹中,然后像这样引用它:

android:background="@drawable/xmas"
于 2012-12-23T18:24:54.173 回答
1

在android中你不应该这样设置背景。稍微搜索一下,你就能得到答案。

  1. 将您的图像 xmas.jpg 放在res/drawable文件夹中。
  2. 更改android:background="C:\Users\Jacob\Downloads\xmas.jpg"android:background="@drawable/xmas"

在 android中,更推荐使用.png并且像我上面所说的那样使用时不需要像 .png 或 .jpg 这样的扩展名。

这将解决您的问题

于 2012-12-23T18:24:59.650 回答
1

要开始工作,您需要将图像复制到<Your_Project>/res/drawable文件夹中,然后将RelativeLayout背景设置为:

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

有关在项目中添加图像的更多信息,请参见

http://developer.android.com/guide/topics/resources/drawable-resource.html

于 2012-12-23T18:26:02.490 回答