2010-07-12 89 views
3

我想要一個頂部有圖像而底部有一些文字的按鈕。圖像和文本都是在運行時決定的,所以我希望能夠爲每個按鈕組合ImageButton的setImageBitmap和Button的setText。帶圖像和文字的Android ImageButton

任何想法?

感謝 克里斯

+0

重複:http://stackoverflow.com/questions/1532876/android-combining-text-image-on-a-button-or-imagebutton – ognian 2010-07-12 06:23:47

+0

我看了看這個問題問過,但我不能與在那裏指定的答案,因爲, 1.我不能設置圖像作爲背景,我想圖像在頂部和文字在底部,而不是圖像上的文字。 2.我需要編程設置的東西,不能使用xml – Chris 2010-07-12 06:32:44

+0

然後檢查我的答案在另一個重複:http://stackoverflow.com/questions/3148713/inflating-a-view-into-button/3149052#3149052 – ognian 2010-07-12 07:13:31

回答

2

我終於找到了我一直在尋找:

setCompoundDrawablesWithIntrinsicBounds讓我分配一個位圖/繪製的按鈕,同時讓我使用的setText。

+0

這是用於Button的文本和圖像,而不是ImageButton,所以這還不是原始的解決方案問題,但一個解決方法。 – 2012-12-12 09:20:50

0

我不認爲有一個簡單的方法來做到這一點。我會創建一個custom-component

+0

它是完全可定義的低谷XML佈局 – ognian 2010-07-12 07:17:20

+0

@ognian:您可以使用XML佈局來定義自定義組件。 – Macarse 2010-07-12 13:34:09

+0

是的,但我想說,沒有必要創建另一個視圖的子類只是爲了有一個圖像和文本的按鈕 – ognian 2010-07-12 17:01:29

1

對於例如:要設置按鈕的頂部位圖圖像,這樣做

button.setCompoundDrawablesWithIntrinsicBounds(空,新BitmapDrawable(的BitmapImage),NULL,NULL);

3

環繞兩個圖像按鈕和文本在....中查看代碼以覆蓋圖像按鈕背景上的文本。即:

<FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="69dp" 
      android:layout_marginTop="96dp" 
      android:background="@drawable/icon" 
      android:scaleType="fitXY" /> 

     <TextView 
      android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="Droid" 
      android:textColor="#ff0000" 
      android:textSize="24dip" > 
     </TextView> 
    </FrameLayout>