Android图像未显示在图像按钮上

9

我遇到了一个问题,我无法将新的图片按钮添加到我的XML文件中。我已经将我的图标添加到Drawable中,但它不能显示在图片按钮中。XML显示错误ImageButton类未找到。我该如何解决?

我在xml中得到了以下错误

以下类找不到: - ImageButton(更改为android.widget.ImageButton,修复构建路径,编辑XML)

这是我的xml

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical" >

       <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="109dp"
        android:src="@drawable/facebook" />

     </RelativeLayout>

1
请上传您的代码。 - Vigbyor
2
如果您在XML中使用了选择器,请确保它的小写为selector而不是Selector。并且请确保图像名称正确,没有大写字符或特殊字符。 - MysticMagicϡ
看看我的新编辑,这是我的 XML。 - user3197109
我已经尝试了清理和构建.. 但是我的drawable图像仍然无法正常工作。 - user3197109
我的问题是我无法在图像按钮上显示图像。当我使用已经存在于drawable文件夹中的图像时,它可以正常工作。 - user3197109
显示剩余6条评论
7个回答

4
为什么不尝试使用简单按钮而不是图像按钮。这是我在一些带有图像的按钮上的代码示例。 android:drawableTop="@drawable/three":这基本上是图片。 android:background="@drawable/buttonstyles":这是一个按钮的样式。不需要使用它。或者我还拥有的其他布局功能。
 <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:background="@drawable/buttonstyles"
        android:drawableTop="@drawable/three"
        android:onClick="goToSettings"
        android:text="Settings" />

我遇到了以下错误:找不到以下类:
  • ImageButton(更改为android.widget.ImageButton,修复构建路径,编辑XML)
- user3197109
尝试创建一个新按钮,并添加drawableTop以链接到图像。看看是否有效。 你发布的这个错误是由我提供的代码产生的吗? - lantonis

2
我正在使用界面构建器,但出现了这个问题。
tools:src="@drawable/ic_play_arrow_white_100dp"

修改代码如下以使图像按钮能够工作并正确显示图片

android:src="@drawable/ic_play_arrow_white_100dp"

1
我曾遇到同样的问题。我的图片格式是.ico,然后在eclipse的图形布局标签中出现了相同的错误信息。我尝试将.ico图片转换为.png格式,然后删除.ico文件,并将.png文件导入项目中。这次成功了。

0

在你的Activity类中,将extend Activity改为extend AppCompatActivity


0

我认为这不是什么大问题。你说的没错,没有任何问题。在我的eclipse中它可以正常工作。

  1. 首先,请检查图像是否正确放置在小写命名约定的drawable文件夹中。或者,如果您只有/res/drawable-xhdpi/中的图像,请也检查一下。可以使用此背景替代src。
  2. 如果您在xml中放置图像时没有得到R无法解析的错误,则首先刷新项目,如果不起作用,则清理整个项目,如果这也不起作用,则重启eclipse。

  3. 如果您重启了eclipse,但仍然无法解决问题,则可能是您的项目库未正确定位。


0

你需要在Java类中将按钮定义为ImageButton。


你的回答可以通过提供更多支持信息来改进。请[编辑]以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

   <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="109dp"
    android:src="@drawable/a" />

   <ImageButton
       android:id="@+id/imageButton3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true"
       android:layout_marginBottom="164dp"
       android:src="@drawable/a" />

 </RelativeLayout>

我的问题是无法在图像按钮上显示图片。当我使用已经存在于drawable文件夹中的图像时,它是有效的。 - user3197109
请尝试从XML中删除以下内容:<?xml version="1.0" encoding="utf-8"?> - Dakshesh Khatri

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