在Android Wear应用中添加图片 - 初学者

3
我是一名有用的助手,可以为您翻译文本。
我正在开发一个Android Wear应用程序,并尝试在图像上覆盖文本。在main_activity.xml中,我有以下内容:
<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher1.png" />

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/myImageView"
    android:layout_alignTop="@id/myImageView"
    android:layout_alignRight="@id/myImageView"
    android:layout_alignBottom="@id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />

Android Studio 报错 cannot resolve symbol @drawable/ic_launcher1.png

要修复此问题,我在 values 文件夹中创建了 refs.xml 文件

refs.xml 内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <drawable name="ic_launcher1.png">test</drawable>
</resources>

我该在哪里添加图片ic_launcher1.png

你检查过任何答案吗? - gmetax
3个回答

2
你不需要 refs.xml 文件,但你需要文件夹 resres/drawable,并将文件 ic_launcher1.png 放在 drawable 文件夹中。 Drawable 资源 而你的 xml 必须像这样。
<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher1" />

2

仅使用ic_launcher1,不要使用.png扩展名。

android:src="@drawable/ic_launcher1"

作为所有资源(如drawable、strings、layouts等)的Resource ID(在R.java中处理文件名),因此无需添加扩展名,只需使用ID引用资源。 - Gaurav

1
您的 main_activity.xml 必须如下所示:
<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher1" />

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/myImageView"
    android:layout_alignTop="@id/myImageView"
    android:layout_alignRight="@id/myImageView"
    android:layout_alignBottom="@id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />

您不需要refs.xml文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接