2010-11-17 106 views

回答

9

正如他所說,使用了ImageButton小部件。將您的圖像文件複製到項目的Res/drawable/目錄中。在XML中,只需進入XML文件的圖形表示(爲了簡單起見),然後單擊您添加的ImageButton小部件,然後轉至其屬性頁並單擊src:字段中的[...]。只需導航到您的圖像文件。另外,請確保您使用的是正確的格式;由於我自己的原因,我傾向於堅持使用.png文件,但它們工作正常。

+0

實際上有幫助的回覆,謝謝!我不知道在哪裏放置圖像或如何指定它們的位置。 – hubatish 2016-02-16 18:56:15

6
<Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="News Feed" 
     android:icon="@drawable/newsfeed" /> 

新聞源則在繪製文件夾

+9

這不起作用。 爲了使按鈕出現的文本和圖標,則使用按鈕類與機器人:drawableLeft屬性: '<按鈕 機器人:layout_width =「WRAP_CONTENT」 機器人:layout_height =「WRAP_CONTENT」 機器人:text =「@ string/button_text」 android:drawableLeft =「@ drawable/button_icon」>' – 2013-02-24 17:25:09

+0

對不起,我的後期重播,但它適用於我 你不分配文本的按鈕,所以它不會出現 和問題是顯示按鈕與圖像,而不是文本:) – 2014-10-14 09:50:31

7

形象你應該嘗試這樣

<Button 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/qrcode"/> 

android:background="@drawable/qrcode"東西會做

+0

[ImageButton](https://developer.android.com/refe rence/android/widget/ImageButton.html)在使用'wrap_content'時總是選擇正確的大小。 「按鈕」可以扭曲圖像,特別是當它們很小時。 – Chilloutman 2016-07-29 16:19:00

3

把你的形象繪製文件夾中。在這裏我的圖像文件名left.png

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_x="118dp" 
    android:layout_y="95dp" 
    android:background="@drawable/left" 
    android:onClick="toast" 
    android:text=" " /> 
12

只需使用ImageButton的查看和設置圖片吧:`

<ImageButton 
    android:id="@+id/searchImageButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:src="@android:drawable/ic_menu_search" /> 
0

你可以在你的Android activity_main.xml中創建的ImageButton和圖像你想放置在您的按鈕只是將該圖像粘貼到您的可繪製文件夾下面是示例代碼供您參考。

<ImageButton 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginBottom="49dp" 
    android:layout_weight="1" 
    android:onClick="prev" 
    android:src="@drawable/prev" 
    />