2012-04-04 51 views
0

我想繪製一個圖像作爲背景的按鈕。 這裏是我的xml:試圖畫一個按鈕

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:background="@drawable/background" 
android:orientation="vertical" > 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="13dp" 
    android:background="@drawable/button_bg" 
    android:text="Break Record" 
    android:textColor="#2c4417" 
    android:textSize="19dp" 
    android:textStyle="bold" > 
</Button> 

</LinearLayout> 

在圖形佈局來看,它看起來像我預期:
enter image description here

但在模擬器上的按鈕可以讓屏幕的整個寬度:
enter image description here

我已經閱讀過規範不止一次,但不能得到我的錯誤,
如何編寫按鈕xml,使按鈕將l在Eclipse的圖形化佈局中,在模擬器(和所有設備上)?

謝謝。

+0

你對所有屏幕都有單獨的背景嗎? – 2012-04-04 06:53:35

+0

如果你打算詢問我是否有單獨的背景圖像的所有決議 - 答案是否定的,我目前有1背景圖像PNG。如果您打算詢問相同的背景是否適用於我的應用中的所有屏幕 - 答案是肯定的。 – 2012-04-04 07:05:41

+0

我指的是按鈕的背景,即@ drawable/button_bg。你應該對每個密度都有所不同。 [看看這個開發指南](http://developer.android.com/guide/practices/screens_support.html) – 2012-04-04 07:09:41

回答

2

按鈕的寬度是由於@drawable/button_bg。如果背景圖像對於不同的密度是恆定的,那麼hdpi會按鈕尺寸較小,另一方面,mdpi和ldpi設備將需要更多寬度來顯示相同​​的按鈕。確認你有不同的背景圖像,並且它們與密度有關。

P.S.使用hdpi,mdpi和ldpi密度分別運行三個不同的仿真器並觀察佈局。

+0

謝謝,但我不能在xml中設置按鈕的寬度和高度,而不是有三個不同的文件? – 2012-04-04 07:08:10

+0

如果你想使用背景圖片,那麼它應該與你的應用將部署的密度合理。我不知道是否設置寬度按鈕可以解決這個問題,但你也應該試試這個。但我建議你爲hdpi mdpi和ldpi製作背景圖片。 – waqaslam 2012-04-04 07:13:36