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

问题描述 投票:4回答:6

我面临的问题是,我无法向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>
android image imagebutton
6个回答
1
投票
  <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
投票

为什么不尝试使用简单的按钮而不是图像按钮。这是我在某些带有图像按钮上的代码。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" />

1
投票

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


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>

0
投票

我认为这不是大问题。您是对的,您发布的内容没有错。在我的日食中工作正常。

  1. 首先检查图像正确放置在具有小写字母命名约定的可绘制文件夹中。或者,如果您仅在/ res / drawable-xhdpi /中使用它,则也请进行检查。可以使用此背景代替src。
  2. << >
  3. 如果重新启动eclipse,则它不起作用,可能是您的项目库位置不正确。

  • 我正在使用界面生成器,由于某种原因,它添加了这个
    Refresh

    更改为此项以使其起作用并在图像按钮中显示图像

    clean

  • 0
    投票
    © www.soinside.com 2019 - 2024. All rights reserved.