【问题标题】:Android Image on Image button not displayedAndroid Image on Image 按钮不显示
【发布时间】:2014-01-22 05:48:58
【问题描述】:

我面临的问题是,我无法将新的图像按钮添加到我的 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>

【问题讨论】:

  • 请上传您的代码
  • 如果您在 xml 中使用了选择器,请确保它的小写 selector 而不是 Selector。并确保图像名称正确。没有大写字符或特殊字符。
  • 查看我的新编辑,这是我的 xml
  • 你尝试过 clean + build 吗?
  • 我已经尝试过清理和构建.. 但它仍然无法处理我在可绘制对象上的图像

标签: android image imagebutton


【解决方案1】:

你为什么不尝试一个简单的按钮而不是图像按钮。这是我在一些带有图像的按钮上的代码。 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)
  • 尝试创建一个新按钮并添加带有图像链接的drawableTop。看看这是否有效。您发布的这个错误是由我链接的代码产生的?
【解决方案2】:
  <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/image1"
        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/yourimage1" />

  <ImageButton
       android:id="@+id/image2"
       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/your image2" />

 </RelativeLayout>

我认为它会起作用,或者确保可绘制文件夹中的图像格式正确

【讨论】:

    【解决方案3】:

    我使用的是界面生成器,由于某种原因,它添加了这个

    tools:src="@drawable/ic_play_arrow_white_100dp"
    

    将其更改为此以使其工作并在图像按钮中显示图像

    android:src="@drawable/ic_play_arrow_white_100dp"
    

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题。我的图像是 .ico,并且在 Eclipse 的“图形布局”选项卡中出现了相同的错误消息。我证明将 .ico 图像转换为 .png 图像,然后删除 .ico 并将 .png 导入到项目中。 成功了。

      【讨论】:

        【解决方案5】:
        <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>
        

        【讨论】:

        • 我的问题是我无法在图像按钮上显示图像。当我使用已在可绘制文件夹中的图像时,它会出错
        • 请尝试从 xml 中删除它
        【解决方案6】:

        我认为这不是什么大问题。你是对的,你张贴没有错。在我的日食中它工作正常。

        1. 首先检查图像是否正确放置在具有小写字母命名约定的可绘制文件夹中。或者,如果您仅在 /res/drawable-xhdpi/ 中有它,请也检查一下。可以使用此背景代替 src。
        2. 1234563 /p>
        3. 如果你重启了eclipse,那么它就不行了,可能是你的项目库没有正确定位。

        【讨论】:

          【解决方案7】:

          在您的 Activity 类中将 extend Activity 更改为 extend AppCompatActivity

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-02-10
            相关资源
            最近更新 更多