我对按钮大小和按钮背景图像大小之间的关系有疑问。
以下是我的代码:
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.play);
button.setBackgroundResource(R.drawable.bg_start);
final String LOGTAG="TEST_LOG";
/* get image resource size */
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.bg_start, options);
/*get button size*/
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.i(LOGTAG, "button size=" + button.getWidth() +"," +button.getHeight());
}
});
Log.i(LOGTAG, "button play image resource size=" + resWidth + "," + resHeight);
}
}
我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
>
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/btn_play_bg"
/>
</RelativeLayout>
btn_play_bg文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
</shape>
运行Activity后,我输出如下:
I / TEST_LOG:按钮播放图像资源大小= 250,250
I / TEST_LOG:按钮大小= 333,333
我的问题是为什么按钮大小不等于图像大小。我认为他们应该是一样的。