2012-06-25 40 views
0

是否可以在按鈕(作爲背景圖像)上添加圖像(視圖)?按鈕上方的圖像android

我正在將iOS應用程序移植到Android上,並且它不是iOS上的問題,但我想知道是否因爲佈局而在Android上採用正確的方法。

編輯:

爲了澄清,檢查此屏幕截圖:

http://a4.mzstatic.com/us/r1000/062/Purple/v4/7c/4b/cd/7c4bcd53-ba55-94d7-a26c-ce1bfe040003/mza_2736801523527387264.320x480-75.jpg

我需要做的左下方按鈕 「菜單」(卡法語)

我需要:

  • 帶背景圖像的按鈕
  • 顯示在從互聯網上載入的按鈕頂部的圖像(一張卡片,有很多不同的新聞卡片,每天都在添加新聞卡片「MIDI PASS」)
  • 本地化的文本按鈕,所以我不能使用Imagebutton類。
+0

如果我理解正確,你想要一個圖像充當按鈕 –

+0

不能imagebutton會爲你工作? –

回答

2

這不是很清楚你想要達到的目標,但下面的情況對您有所幫助:

更新

看看您的更新要求,最好使用custom component來達到您想要的效果。

+0

好吧,我會嘗試自定義組件謝謝 –

0

是的,這是可能的。

使用的ImageButton然後....

設置你的android:SRC = 「@繪製/前景圖像」 設置你的android:背景= 「@繪製/背景圖片」

所以,如果你想要一個蘋果的背景圖像和一個三維詞「蘋果」爲您的前景圖像。

1

你的問題是有些不清楚,但是從我的理解,以下可能會爲你工作:

 <ImageButton android:id="@+id/imgButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/bg_image" 
      android:src="@drawable/top_image"/> 

希望這會有所幫助。

UPDATE: 如果你的背景是常見的,那麼你可以設置使用下面的代碼位圖:

((ImageButton)findViewById(R.id.imgButton)).setImageBitmap(bmp); 

在這裏,你將需要獲得BMP變量名片圖像的位圖。

+0

請看這個截圖:http://a4.mzstatic.com/us/r1000/062/Purple/v4/7c/4b/cd/7c4bcd53-ba55-94d7-a26c-ce1bfe040003 /mza_2736801523527387264.320x480-75.jpg,左下方的按鈕就是我需要的。它是一個僅在某些情況下顯示的按鈕,表示需要卡,並且有許多不同的卡,所以我無法使用圖像資源。我不得不在代碼中更改卡片圖像。 –

+0

並且是所有人的共同背景? – Veer

0

你可以嘗試這樣的事:

首先,你在res /繪製/文件夾中創建按鈕的選擇(我們稱之爲selector_button.xml):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:drawable="@drawable/image_resource_for_button_pressed" 
      android:state_pressed="true" /> 
    <item android:drawable="@drawable/image_resource_for_button_pressed" 
      android:state_focused="true" /> 
    <item android:drawable="@drawable/image_resource_for_button_normal" /> 
</selector> 

在這裏,您可以定義爲和android:drawable不僅僅是@drawable的,而是@color的或@layout的。如果您想要更復雜的佈局,則應使用例如RelativeLayout來定義一個具有按鈕背景圖像和另一個圖像的頂部圖像。

爲了做到這一點,你必須在你的res/drawable /文件夾中有image_resource_for_button_pressed.png(用於按下和聚焦狀態)和image_resource_for_button_normal.png(用於正常狀態)。

之後,您創建一個按鈕,像這樣:

<Button 
    android:id="@+id/aButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/selector_button" 
    android:text="Hardcoded string" /> 

這種方法可以幫助你保持代碼的可讀性,因爲你剛纔提取的圖像資源的變更爲.xml文件。