2014-01-22 78 views
2

我面臨的問題是,我不能將新的圖像按鈕添加到我的XML文件中。我將我的圖標添加到Drawable,但它無法顯示在圖像按鈕中。 XML顯示找不到錯誤的ImageButton類。我該如何解決這個問題?Android Image on Image按鈕不顯示

我在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'。並確保圖像名稱是正確的。沒有大寫字母或特殊字符。 –

+0

查看我的新編輯,這裏是我的xml – doubter

回答

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> 

我認爲它wrks,或確保圖像是正確的格式在繪製文件夾

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

我的問題是我無法顯示圖像按鈕上的圖像。當我使用已經在可繪製文件夾中的圖像 – doubter

+0

<?xml version =「 1.0「encoding =」utf-8「?> plz嘗試從xml中刪除它 – DaxeshKhatri

0

我覺得這不是大問題。你發佈對你沒有錯。在我的月食中,它工作正常。

  1. 首先檢查圖像是否正確放入帶有小寫字母命名約定的可繪製文件夾中。或者如果你只有它/ res/drawable-xhdpi /請檢查這一點。可以使用這個背景而不是src。
  2. 如果你不是沒有得到R不能解決的話把圖片放到xml中,那麼先寫Refresh項目,如果不行的話clean整個項目,如果這個也不行的話restart eclipse。

  3. 如果你重新啓動eclipse,那麼它不工作,可能是你的項目庫沒有正確定位。

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" /> 
+0

我得到了錯誤以下類無法找到: - ImageButton(更改爲android.widget.ImageButton,修復構建路徑,編輯XML) – doubter

+0

嘗試製作一個新按鈕並添加帶圖像鏈接的drawableTop。看看是否有效。 您發佈的這個錯誤是由我鏈接的代碼產生的? – lantonis

1

我有同樣的問題。我的圖像是.ico,並且在eclipse中的圖形佈局選項卡中有相同的錯誤信息。我證明將.ico圖像轉換爲.png圖像,然後刪除了.ico,並將.png導入到項目中。 這是成功的。