2012-04-04 49 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